Skip to content

Commit

Permalink
Merge pull request #674 from DataDog/marcosaia/RUM-4060/fix-datadog-c…
Browse files Browse the repository at this point in the history
…i-path

[RUM-4060] Improved datadog-ci exec path resolving
  • Loading branch information
marco-saia-datadog authored Jun 12, 2024
2 parents 298141b + c0bf446 commit 99914f0
Showing 1 changed file with 64 additions and 1 deletion.
65 changes: 64 additions & 1 deletion packages/core/datadog-sourcemaps.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ afterEvaluate {

def execCommand = { jsBundleFile ->
return [
"$reactConfig.root/node_modules/.bin/datadog-ci",
"${getDatadogCiExecPath(reactConfig)}",
"react-native",
"upload",
"--platform",
Expand Down Expand Up @@ -97,6 +97,69 @@ afterEvaluate {
}
}

/**
* We use a function here to resolve the datadog-ci executable path.
* If DATADOG_CI_EXEC env variable is defined, it will be returned (if valid).
*/
private def getDatadogCiExecPath(reactConfig) {
def defaultPath = "${reactConfig.root}/node_modules/.bin/datadog-ci"

// Try to retrieve the path from ENV variable
def envPath = System.getenv('DATADOG_CI_EXEC')
if (envPath != null) {
if (isValidDatadogCiExec(envPath)) {
return envPath
} else {
println("WARNING: Ignoring DATADOG_CI_EXEC as it does not point to a valid datadog-ci executable")
}
}

def nodeExecutable = Os.isFamily(Os.FAMILY_WINDOWS) ? 'node.exe' : 'node'
def nodeScript = '''
const path = require('path');
const modulePath = require.resolve('@datadog/datadog-ci/package.json');
const nodeModulesDir = path.resolve(path.dirname(modulePath), '../../');
const datadogCiExec = path.join(nodeModulesDir, '.bin', 'datadog-ci');
console.log(datadogCiExec);
'''

def stdout = new ByteArrayOutputStream()
def process = new ProcessBuilder(nodeExecutable)
.redirectErrorStream(true)
.start()

process.outputStream.withWriter { writer ->
writer << nodeScript
}

process.inputStream.eachLine { line ->
stdout << line << '\n'
}

process.waitFor()

def resolvedPath = stdout.toString().trim()
if (isValidDatadogCiExec(resolvedPath)) {
return resolvedPath
} else {
println("WARNING: Could not resolve datadog-ci executable path, falling back to default: ${defaultPath}")
}

return defaultPath
}

/**
* Function to validate datadog-ci executable path.
*/
private def isValidDatadogCiExec(String path) {
def file = new File(path)
if (!file.exists() || !file.canExecute()) {
return false
}

return true
}

/**
* We use a function here to resolve the correct bundle location after the bundle task
* is over, as its location can depend on the Android Gradle Plugin version.
Expand Down

0 comments on commit 99914f0

Please sign in to comment.