@@ -15,11 +15,13 @@ project.afterEvaluate {
15
15
def flavor = name. substring(start, end). uncapitalize()
16
16
def defaultVersion = getDefaultVersion(flavor)
17
17
18
- task. finalizedBy createUploadSourcemapsTask(flavor, defaultVersion. name, defaultVersion. code)
18
+
19
+
20
+ task. finalizedBy createUploadSourcemapsTask(flavor, defaultVersion. name, defaultVersion. code,task)
19
21
}
20
22
}
21
23
22
- Task createUploadSourcemapsTask (String flavor , String defaultVersionName , String defaultVersionCode ) {
24
+ Task createUploadSourcemapsTask (String flavor , String defaultVersionName , String defaultVersionCode , Task task ) {
23
25
def name = ' uploadSourcemaps' + flavor. capitalize()
24
26
25
27
// Don't recreate the task if it already exists.
@@ -39,18 +41,26 @@ Task createUploadSourcemapsTask(String flavor, String defaultVersionName, String
39
41
try {
40
42
def appProject = project(' :app' )
41
43
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} "
43
46
44
47
def jsProjectDir = rootDir. parentFile
45
48
def instabugDir = new File ([' node' , ' -p' , ' require.resolve("instabug-reactnative/package.json")' ]. execute(null , rootDir). text. trim()). getParentFile()
46
49
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
+
49
57
def appToken = resolveVar(' App Token' , ' INSTABUG_APP_TOKEN' , inferredToken)
50
58
51
59
def versionName = resolveVar(' Version Name' , ' INSTABUG_VERSION_NAME' , defaultVersionName)
52
60
def versionCode = resolveVar(' Version Code' , ' INSTABUG_VERSION_CODE' , defaultVersionCode)
53
61
62
+ println " 📦 Uploading with versionName=${ versionName} , versionCode=${ versionCode} , appToken=${ appToken.take(5)} ..."
63
+
54
64
exec {
55
65
def osCompatibility = Os . isFamily(Os . FAMILY_WINDOWS ) ? [' cmd' , ' /c' ] : []
56
66
def args = [
@@ -67,34 +77,57 @@ Task createUploadSourcemapsTask(String flavor, String defaultVersionName, String
67
77
} catch (exception) {
68
78
project. logger. error " Failed to upload source map file.\n " +
69
79
" Reason: ${ exception.message} "
80
+ throw exception
81
+
70
82
}
71
83
}
72
84
}
73
85
74
86
return provider. get()
75
87
}
76
88
77
- File getSourceMapFile (File appDir , String flavor ) {
89
+ File getSourceMapFile (File appDir , String flavor , Task task ) {
78
90
def defaultFlavorPath = flavor. empty ? ' release' : " ${ flavor} Release"
79
91
def defaultSourceMapDest = " build/generated/sourcemaps/react/${ defaultFlavorPath} /index.android.bundle.map"
80
92
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
+ }
81
112
82
113
if (defaultSourceMapFile. exists()) {
83
114
return defaultSourceMapFile
84
115
}
85
116
86
117
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
88
120
}
89
121
90
122
def fallbackSourceMapDest = " build/generated/sourcemaps/react/${ flavor} /release/index.android.bundle.map"
91
123
def fallbackSourceMapFile = new File (appDir, fallbackSourceMapDest)
92
124
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 " +
94
126
" Falling back to ${ fallbackSourceMapFile.absolutePath} ."
95
127
96
128
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
98
131
}
99
132
100
133
return fallbackSourceMapFile
@@ -155,14 +188,56 @@ String resolveVar(String name, String envKey, String defaultValue) {
155
188
return value
156
189
}
157
190
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
+
158
220
static String executeShellScript (File script , File workingDir ) {
159
221
if (Os . isFamily(Os . FAMILY_WINDOWS )) {
160
222
return null
161
223
}
162
224
225
+ if (! script. canExecute()) {
226
+ // Try to set executable permission
227
+ script. setExecutable(true )
228
+ }
229
+
163
230
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
164
234
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
+ }
166
241
167
- return process ?. exitValue() == 0 ? output. toString(). trim() : null
242
+ return output. toString(). trim()
168
243
}
0 commit comments