Skip to content

Commit 6c23d2a

Browse files
committed
initial version
0 parents  commit 6c23d2a

File tree

77 files changed

+18040
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+18040
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pbxproj -text

.gitignore

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# OSX
2+
#
3+
.DS_Store
4+
5+
# node.js
6+
#
7+
node_modules/
8+
npm-debug.log
9+
yarn-error.log
10+
11+
# Xcode
12+
#
13+
build/
14+
*.pbxuser
15+
!default.pbxuser
16+
*.mode1v3
17+
!default.mode1v3
18+
*.mode2v3
19+
!default.mode2v3
20+
*.perspectivev3
21+
!default.perspectivev3
22+
xcuserdata
23+
*.xccheckout
24+
*.moved-aside
25+
DerivedData
26+
*.hmap
27+
*.ipa
28+
*.xcuserstate
29+
project.xcworkspace
30+
31+
# Android/IntelliJ
32+
#
33+
build/
34+
.idea
35+
.gradle
36+
local.properties
37+
*.iml
38+
39+
# BUCK
40+
buck-out/
41+
\.buckd/
42+
*.keystore

.npmignore

Whitespace-only changes.

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# react-native-create-guid
2+
3+
Minimal library to create Guids in React Native. It uses the native implementations to generate one and send it over to JS.
4+
5+
## Why?
6+
7+
When faced with the problem of generating a GUID in React Native, I found that there were no current good solutions. Relying on `Math.random` didn't seem like a good idea, and the other alternatives found like [react-native-uuid](https://www.npmjs.com/package/react-native-uuid) that are just ports of node.js libraries (needed to install other libraries buffer).
8+
9+
Since native platforms already have ways to create GUIDs that are useful, creating a simple native library would be enough to use them in React-Native.
10+
11+
## Getting started
12+
13+
`$ npm install react-native-create-guid --save`
14+
15+
### Mostly automatic installation
16+
17+
**RN < 0.60:**
18+
19+
`$ react-native link react-native-create-guid`
20+
21+
**RN >= 0.60:**
22+
23+
`$ cd ios && pod install`
24+
25+
### Manual installation
26+
27+
#### iOS
28+
29+
1. In XCode, in the project navigator, right click `Libraries``Add Files to [your project's name]`
30+
2. Go to `node_modules``react-native-create-guid` and add `CreateGuid.xcodeproj`
31+
3. In XCode, in the project navigator, select your project. Add `libCreateGuid.a` to your project's `Build Phases``Link Binary With Libraries`
32+
4. Run your project (`Cmd+R`)<
33+
34+
#### Android
35+
36+
1. Open up `android/app/src/main/java/[...]/MainApplication.java`
37+
38+
- Add `import com.reactnative.createguid.CreateGuidPackage;` to the imports at the top of the file
39+
- Add `new CreateGuidPackage()` to the list returned by the `getPackages()` method
40+
41+
2. Append the following lines to `android/settings.gradle`:
42+
```
43+
include ':react-native-create-guid'
44+
project(':react-native-create-guid').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-create-guid/android')
45+
```
46+
3. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
47+
```
48+
compile project(':react-native-create-guid')
49+
```
50+
51+
## Usage
52+
53+
Check the example under `example` folder for it's usage.
54+
55+
```javascript
56+
import { createGuid } from "react-native-create-guid";
57+
58+
// in an async function;
59+
const guid = await createGuid();
60+
61+
// using promises
62+
createGuid().then(guid => console.log(guid));
63+
```

android/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
README
2+
======
3+
4+
If you want to publish the lib as a maven dependency, follow these steps before publishing a new version to npm:
5+
6+
1. Be sure to have the Android [SDK](https://developer.android.com/studio/index.html) and [NDK](https://developer.android.com/ndk/guides/index.html) installed
7+
2. Be sure to have a `local.properties` file in this folder that points to the Android SDK and NDK
8+
```
9+
ndk.dir=/Users/{username}/Library/Android/sdk/ndk-bundle
10+
sdk.dir=/Users/{username}/Library/Android/sdk
11+
```
12+
3. Delete the `maven` folder
13+
4. Run `./gradlew installArchives`
14+
5. Verify that latest set of generated files is in the maven folder with the correct version number

android/build.gradle

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// android/build.gradle
2+
3+
def safeExtGet(prop, fallback) {
4+
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
5+
}
6+
7+
buildscript {
8+
// The Android Gradle plugin is only required when opening the android folder stand-alone.
9+
// This avoids unnecessary downloads and potential conflicts when the library is included as a
10+
// module dependency in an application project.
11+
if (project == rootProject) {
12+
repositories {
13+
google()
14+
jcenter()
15+
}
16+
dependencies {
17+
classpath 'com.android.tools.build:gradle:3.4.1'
18+
}
19+
}
20+
}
21+
22+
apply plugin: 'com.android.library'
23+
apply plugin: 'maven'
24+
25+
// Matches values in recent template from React Native 0.59 / 0.60
26+
// https://github.com/facebook/react-native/blob/0.59-stable/template/android/build.gradle#L5-L9
27+
// https://github.com/facebook/react-native/blob/0.60-stable/template/android/build.gradle#L5-L9
28+
def DEFAULT_COMPILE_SDK_VERSION = 28
29+
def DEFAULT_BUILD_TOOLS_VERSION = "28.0.3"
30+
def DEFAULT_MIN_SDK_VERSION = 16
31+
def DEFAULT_TARGET_SDK_VERSION = 28
32+
33+
android {
34+
compileSdkVersion safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)
35+
buildToolsVersion safeExtGet('buildToolsVersion', DEFAULT_BUILD_TOOLS_VERSION)
36+
defaultConfig {
37+
minSdkVersion safeExtGet('minSdkVersion', DEFAULT_MIN_SDK_VERSION)
38+
targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
39+
versionCode 1
40+
versionName "1.0"
41+
}
42+
lintOptions {
43+
abortOnError false
44+
}
45+
}
46+
47+
repositories {
48+
mavenLocal()
49+
maven {
50+
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
51+
url "$rootDir/../node_modules/react-native/android"
52+
}
53+
maven {
54+
// Android JSC is installed from npm
55+
url "$rootDir/../node_modules/jsc-android/dist"
56+
}
57+
google()
58+
jcenter()
59+
}
60+
61+
dependencies {
62+
// ref:
63+
// https://github.com/facebook/react-native/blob/0.61-stable/template/android/app/build.gradle#L192
64+
//noinspection GradleDynamicVersion
65+
implementation 'com.facebook.react:react-native:+' // From node_modules
66+
}
67+
68+
def configureReactNativePom(def pom) {
69+
def packageJson = new groovy.json.JsonSlurper().parseText(file('../package.json').text)
70+
71+
pom.project {
72+
name packageJson.title
73+
artifactId packageJson.name
74+
version = packageJson.version
75+
group = "com.reactnative.createguid"
76+
description packageJson.description
77+
url packageJson.repository.baseUrl
78+
79+
licenses {
80+
license {
81+
name packageJson.license
82+
url packageJson.repository.baseUrl + '/blob/master/' + packageJson.licenseFilename
83+
distribution 'repo'
84+
}
85+
}
86+
87+
developers {
88+
developer {
89+
id packageJson.author.username
90+
name packageJson.author.name
91+
}
92+
}
93+
}
94+
}
95+
96+
afterEvaluate { project ->
97+
// some Gradle build hooks ref:
98+
// https://www.oreilly.com/library/view/gradle-beyond-the/9781449373801/ch03.html
99+
task androidJavadoc(type: Javadoc) {
100+
source = android.sourceSets.main.java.srcDirs
101+
classpath += files(android.bootClasspath)
102+
classpath += files(project.getConfigurations().getByName('compile').asList())
103+
include '**/*.java'
104+
}
105+
106+
task androidJavadocJar(type: Jar, dependsOn: androidJavadoc) {
107+
classifier = 'javadoc'
108+
from androidJavadoc.destinationDir
109+
}
110+
111+
task androidSourcesJar(type: Jar) {
112+
classifier = 'sources'
113+
from android.sourceSets.main.java.srcDirs
114+
include '**/*.java'
115+
}
116+
117+
android.libraryVariants.all { variant ->
118+
def name = variant.name.capitalize()
119+
task "jar${name}"(type: Jar, dependsOn: variant.javaCompile) {
120+
from variant.javaCompile.destinationDir
121+
}
122+
}
123+
124+
artifacts {
125+
archives androidSourcesJar
126+
archives androidJavadocJar
127+
}
128+
129+
task installArchives(type: Upload) {
130+
configuration = configurations.archives
131+
repositories.mavenDeployer {
132+
// Deploy to react-native-event-bridge/maven, ready to publish to npm
133+
repository url: "file://${projectDir}/../android/maven"
134+
configureReactNativePom pom
135+
}
136+
}
137+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.reactnative.createguid">
3+
4+
</manifest>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.reactnative.createguid;
2+
3+
import java.util.UUID;
4+
5+
import com.facebook.react.bridge.Promise;
6+
import com.facebook.react.bridge.ReactApplicationContext;
7+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
8+
import com.facebook.react.bridge.ReactMethod;
9+
import com.facebook.react.bridge.Callback;
10+
11+
public class CreateGuidModule extends ReactContextBaseJavaModule {
12+
13+
private final ReactApplicationContext reactContext;
14+
15+
public CreateGuidModule(ReactApplicationContext reactContext) {
16+
super(reactContext);
17+
this.reactContext = reactContext;
18+
}
19+
20+
@Override
21+
public String getName() {
22+
return "CreateGuid";
23+
}
24+
25+
@ReactMethod
26+
public void createGuid(Promise promise) {
27+
UUID uuid = UUID.randomUUID();
28+
String uuidInString = uuid.toString();
29+
promise.resolve(uuidInString);
30+
}
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.reactnative.createguid;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
import com.facebook.react.ReactPackage;
8+
import com.facebook.react.bridge.NativeModule;
9+
import com.facebook.react.bridge.ReactApplicationContext;
10+
import com.facebook.react.uimanager.ViewManager;
11+
import com.facebook.react.bridge.JavaScriptModule;
12+
13+
public class CreateGuidPackage implements ReactPackage {
14+
@Override
15+
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
16+
return Arrays.<NativeModule>asList(new CreateGuidModule(reactContext));
17+
}
18+
19+
@Override
20+
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
21+
return Collections.emptyList();
22+
}
23+
}

example/.buckconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
[android]
3+
target = Google Inc.:Google APIs:23
4+
5+
[maven_repositories]
6+
central = https://repo1.maven.org/maven2

0 commit comments

Comments
 (0)