diff --git a/packages/react-native-codegen/android/build.gradle b/packages/react-native-codegen/android/build.gradle index e34e9df6c999de..e84754762c4112 100644 --- a/packages/react-native-codegen/android/build.gradle +++ b/packages/react-native-codegen/android/build.gradle @@ -5,6 +5,8 @@ * LICENSE file in the root directory of this source tree. */ +import org.apache.tools.ant.taskdefs.condition.Os + buildscript { repositories { mavenLocal() @@ -42,5 +44,23 @@ task('buildCodegenCLI', type: Exec) { nodeModulesDir.mkdirs(); outputs.dirs(libDir, nodeModulesDir) - commandLine("$codegenRoot/scripts/oss/build.sh") + if (Os.isFamily(Os.FAMILY_WINDOWS)) { + // Convert path to Linux format: use canonical path to strip it off relative elements in the middle of the string. + // Then replace baskslashes with slashes, remove leading colon, add leading slash. + // Eg. D:\path1\sub2/.. -> /D/path1/path2 + String canonicalPath = new File(codegenRoot).getCanonicalPath() + String linuxPath = canonicalPath.replace('\\', '/'); + linuxPath = linuxPath.replace(':', '') + linuxPath = '/' + linuxPath + + // Get the location of bash in the system; assume environment variable created to store it. + String bashHome = "$System.env.REACT_WINDOWS_BASH" + if (bashHome == null) { + throw new GradleException("REACT_WINDOWS_BASH is not defined.") + } + commandLine(bashHome, "-c", "$linuxPath/scripts/oss/build.sh") + } + else { + commandLine("$codegenRoot/scripts/oss/build.sh") + } } diff --git a/packages/react-native-codegen/android/gradlePlugin-build/gradlePlugin/src/main/java/com/facebook/react/codegen/plugin/CodegenPlugin.java b/packages/react-native-codegen/android/gradlePlugin-build/gradlePlugin/src/main/java/com/facebook/react/codegen/plugin/CodegenPlugin.java index 1d27bf3f8d40ac..dc760c3f17088c 100644 --- a/packages/react-native-codegen/android/gradlePlugin-build/gradlePlugin/src/main/java/com/facebook/react/codegen/plugin/CodegenPlugin.java +++ b/packages/react-native-codegen/android/gradlePlugin-build/gradlePlugin/src/main/java/com/facebook/react/codegen/plugin/CodegenPlugin.java @@ -33,6 +33,8 @@ public void apply(final Project project) { final File generatedSchemaFile = new File(generatedSrcDir, "schema.json"); // 2. Task: produce schema from JS files. + String os = System.getProperty("os.name").toLowerCase(); + project .getTasks() .register( @@ -62,7 +64,7 @@ public void apply(final Project project) { ImmutableList execCommands = new ImmutableList.Builder() - .add("yarn") + .add(os.contains("windows") ? "yarn.cmd" : "yarn") .addAll(ImmutableList.copyOf(extension.nodeExecutableAndArgs)) .add(extension.codegenGenerateSchemaCLI().getAbsolutePath()) .add(generatedSchemaFile.getAbsolutePath()) @@ -96,7 +98,7 @@ public void apply(final Project project) { ImmutableList execCommands = new ImmutableList.Builder() - .add("yarn") + .add(os.contains("windows") ? "yarn.cmd" : "yarn") .addAll(ImmutableList.copyOf(extension.nodeExecutableAndArgs)) .add(extension.codegenGenerateNativeModuleSpecsCLI().getAbsolutePath()) .add("android") diff --git a/packages/react-native-codegen/scripts/oss/build.sh b/packages/react-native-codegen/scripts/oss/build.sh index 7e22d6bbd6040a..b5913f9f1d6a1e 100755 --- a/packages/react-native-codegen/scripts/oss/build.sh +++ b/packages/react-native-codegen/scripts/oss/build.sh @@ -32,12 +32,22 @@ if [[ ${FBSOURCE_ENV:-0} -eq 1 ]]; then "$YARN_BINARY" run build popd >/dev/null + else # Run yarn install in a separate tmp dir to avoid conflict with the rest of the repo. # Note: OSS-only. TMP_DIR=$(mktemp -d) - cp -R "$CODEGEN_DIR/." "$TMP_DIR" + # On Windows this script gets run by a seprate Git Bash instance, which cannot perform the copy + # due to file locks created by the host process. Need to exclude .lock files while copying. + # Using in-memory tar operation because piping `find` and `grep` doesn't preserve folder structure + # during recursive copying, and `rsync` is not installed by default in Git Bash. + # As an added benefit, blob copy is faster. + if [ "$OSTYPE" = "msys" ] || [ "$OSTYPE" = "cygwin" ]; then + tar cf - --exclude='*.lock' "$CODEGEN_DIR" | (cd "$TMP_DIR" && tar xvf - ); + else + cp -R "$CODEGEN_DIR/." "$TMP_DIR"; + fi pushd "$TMP_DIR" >/dev/null