@@ -15,11 +15,13 @@ project.afterEvaluate {
1515 def flavor = name. substring(start, end). uncapitalize()
1616 def defaultVersion = getDefaultVersion(flavor)
1717
18- task. finalizedBy createUploadSourcemapsTask(flavor, defaultVersion. name, defaultVersion. code)
18+
19+
20+ task. finalizedBy createUploadSourcemapsTask(flavor, defaultVersion. name, defaultVersion. code,task)
1921 }
2022}
2123
22- Task createUploadSourcemapsTask (String flavor , String defaultVersionName , String defaultVersionCode ) {
24+ Task createUploadSourcemapsTask (String flavor , String defaultVersionName , String defaultVersionCode , Task task ) {
2325 def name = ' uploadSourcemaps' + flavor. capitalize()
2426
2527 // Don't recreate the task if it already exists.
@@ -39,18 +41,26 @@ Task createUploadSourcemapsTask(String flavor, String defaultVersionName, String
3941 try {
4042 def appProject = project(' :app' )
4143 def appDir = appProject. projectDir
42- def sourceMapFile = getSourceMapFile(appDir, flavor)
44+ def sourceMapFile = getSourceMapFile(appDir, flavor,task)
45+ println " ✅ Resolved sourcemap file path: ${ sourceMapFile.absolutePath} "
4346
4447 def jsProjectDir = rootDir. parentFile
4548 def instabugDir = new File ([' node' , ' -p' , ' require.resolve("instabug-reactnative/package.json")' ]. execute(null , rootDir). text. trim()). getParentFile()
4649
47- def tokenShellFile = new File (instabugDir, ' scripts/find-token.sh' )
48- def inferredToken = executeShellScript(tokenShellFile, jsProjectDir)
50+ def tokenJsFile = new File (instabugDir, ' scripts/find-token.js' )
51+ def inferredToken = executeNodeScript(tokenJsFile, jsProjectDir)
52+
53+ if (! inferredToken) {
54+ throw new GradleException (" ❌ Unable to infer Instabug token from script: ${ tokenShellFile.absolutePath} " )
55+ }
56+
4957 def appToken = resolveVar(' App Token' , ' INSTABUG_APP_TOKEN' , inferredToken)
5058
5159 def versionName = resolveVar(' Version Name' , ' INSTABUG_VERSION_NAME' , defaultVersionName)
5260 def versionCode = resolveVar(' Version Code' , ' INSTABUG_VERSION_CODE' , defaultVersionCode)
5361
62+ println " 📦 Uploading with versionName=${ versionName} , versionCode=${ versionCode} , appToken=${ appToken.take(5)} ..."
63+
5464 exec {
5565 def osCompatibility = Os . isFamily(Os . FAMILY_WINDOWS ) ? [' cmd' , ' /c' ] : []
5666 def args = [
@@ -67,34 +77,57 @@ Task createUploadSourcemapsTask(String flavor, String defaultVersionName, String
6777 } catch (exception) {
6878 project. logger. error " Failed to upload source map file.\n " +
6979 " Reason: ${ exception.message} "
80+ throw exception
81+
7082 }
7183 }
7284 }
7385
7486 return provider. get()
7587}
7688
77- File getSourceMapFile (File appDir , String flavor ) {
89+ File getSourceMapFile (File appDir , String flavor , Task task ) {
7890 def defaultFlavorPath = flavor. empty ? ' release' : " ${ flavor} Release"
7991 def defaultSourceMapDest = " build/generated/sourcemaps/react/${ defaultFlavorPath} /index.android.bundle.map"
8092 def defaultSourceMapFile = new File (appDir, defaultSourceMapDest)
93+ def props = task. getProperties()
94+
95+ def bundleAssetName = props. containsKey(" bundleAssetName" ) ? props. bundleAssetName?. getOrNull() : null
96+ def jsSourceMapsDir = props. containsKey(" jsSourceMapsDir" ) ? props. jsSourceMapsDir?. getOrNull() : null
97+ def jsIntermediateSourceMapsDir = props. containsKey(" jsIntermediateSourceMapsDir" ) ? props. jsIntermediateSourceMapsDir?. getOrNull() : null
98+
99+ if (bundleAssetName && jsSourceMapsDir) {
100+ def outputSourceMap = new File (jsSourceMapsDir. asFile. absolutePath, " ${ bundleAssetName} .map" )
101+ if (outputSourceMap. exists()) {
102+ return outputSourceMap
103+ }
104+ }
105+
106+ if (bundleAssetName && jsIntermediateSourceMapsDir) {
107+ def packagerOutputSourceMap = new File (jsIntermediateSourceMapsDir. asFile. absolutePath, " ${ bundleAssetName} .packager.map" )
108+ if (packagerOutputSourceMap. exists()) {
109+ return packagerOutputSourceMap
110+ }
111+ }
81112
82113 if (defaultSourceMapFile. exists()) {
83114 return defaultSourceMapFile
84115 }
85116
86117 if (flavor. empty) {
87- throw new InvalidUserDataException (" Unable to find source map file at: ${ defaultSourceMapFile.absolutePath} ." )
118+ println " Source map file not found at: ${ defaultSourceMapFile.absolutePath} . Skipping."
119+ return null
88120 }
89121
90122 def fallbackSourceMapDest = " build/generated/sourcemaps/react/${ flavor} /release/index.android.bundle.map"
91123 def fallbackSourceMapFile = new File (appDir, fallbackSourceMapDest)
92124
93- project . logger . info " Unable to find source map file at: ${ defaultSourceMapFile.absolutePath} .\n " +
125+ println " Unable to find source map file at: ${ defaultSourceMapFile.absolutePath} .\n " +
94126 " Falling back to ${ fallbackSourceMapFile.absolutePath} ."
95127
96128 if (! fallbackSourceMapFile. exists()) {
97- throw new InvalidUserDataException (" Unable to find source map file at: ${ fallbackSourceMapFile.absolutePath} either." )
129+ println " Fallback source map file not found at: ${ fallbackSourceMapFile.absolutePath} . Skipping."
130+ return null
98131 }
99132
100133 return fallbackSourceMapFile
@@ -155,14 +188,56 @@ String resolveVar(String name, String envKey, String defaultValue) {
155188 return value
156189}
157190
191+ static String executeNodeScript (File script , File workingDir ) {
192+ if (! script. exists()) {
193+ println " Script not found: ${ script.absolutePath} "
194+ return null
195+ }
196+
197+ def output = new StringBuffer ()
198+ def error = new StringBuffer ()
199+
200+ try {
201+ def process = [' node' , script. getAbsolutePath()]. execute(null , workingDir)
202+ process. waitForProcessOutput(output, error)
203+
204+ if (process. exitValue() != 0 ) {
205+ println " Script failed with exit code ${ process.exitValue()} "
206+ println " Standard Error:\n ${ error.toString().trim()} "
207+ println " Standard Output:\n ${ output.toString().trim()} "
208+ return null
209+ }
210+
211+ return output. toString(). trim()
212+
213+ } catch (Exception e) {
214+ println " Exception while executing Node.js script: ${ e.message} "
215+ e. printStackTrace()
216+ return null
217+ }
218+ }
219+
158220static String executeShellScript (File script , File workingDir ) {
159221 if (Os . isFamily(Os . FAMILY_WINDOWS )) {
160222 return null
161223 }
162224
225+ if (! script. canExecute()) {
226+ // Try to set executable permission
227+ script. setExecutable(true )
228+ }
229+
163230 def output = new StringBuffer ()
231+ def error = new StringBuffer ()
232+
233+ // Using 'sh' instead of './' to avoid needing exec permission, but keeping chmod above just in case
164234 def process = [' sh' , script. getAbsolutePath()]. execute(null , workingDir)
165- process?. waitForProcessOutput(output, new StringBuffer ())
235+ process?. waitForProcessOutput(output, error)
236+
237+ if (process. exitValue() != 0 ) {
238+ println " Error running script: ${ error.toString().trim()} "
239+ return null
240+ }
166241
167- return process ?. exitValue() == 0 ? output. toString(). trim() : null
242+ return output. toString(). trim()
168243}
0 commit comments