Skip to content

Commit b10b6d6

Browse files
authored
Merge pull request #1472 from NativeScript/trifonov/improve-build-errors-output
Show only the error message when build tool fails
2 parents 15ea13c + 28d9536 commit b10b6d6

File tree

11 files changed

+196
-60
lines changed

11 files changed

+196
-60
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ bin/
2020
.settings
2121

2222
.classpath
23-
android-runtime.iml
23+
android-runtime.iml
24+
test-app/build-tools/*.log

build.gradle

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,20 @@ task copyFilesToProjectTemeplate {
287287
into "$DIST_FRAMEWORK_PATH"
288288
}
289289
copy {
290-
from "$TEST_APP_PATH/paths.gradle"
291-
into "$DIST_FRAMEWORK_PATH"
290+
from "$TEST_APP_PATH/gradle-helpers/paths.gradle"
291+
into "$DIST_FRAMEWORK_PATH/gradle-helpers"
292292
}
293293
copy {
294-
from "$TEST_APP_PATH/user_properties_reader.gradle"
295-
into "$DIST_FRAMEWORK_PATH"
294+
from "$TEST_APP_PATH/gradle-helpers/user_properties_reader.gradle"
295+
into "$DIST_FRAMEWORK_PATH/gradle-helpers"
296+
}
297+
copy {
298+
from "$TEST_APP_PATH/app/gradle-helpers/CustomExecutionLogger.gradle"
299+
into "$DIST_FRAMEWORK_PATH/app/gradle-helpers"
300+
}
301+
copy {
302+
from "$TEST_APP_PATH/app/gradle-helpers/BuildToolTask.gradle"
303+
into "$DIST_FRAMEWORK_PATH/app/gradle-helpers"
296304
}
297305
copy {
298306
from "$TEST_APP_PATH/gradle"

test-app/app/build.gradle

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@ import java.nio.file.Files
2727
import java.nio.file.Paths
2828
import java.nio.file.StandardCopyOption
2929
import java.security.MessageDigest
30+
import org.gradle.internal.logging.text.StyledTextOutputFactory
31+
import static org.gradle.internal.logging.text.StyledTextOutput.Style
3032

3133
apply plugin: "com.android.application"
34+
apply from: "gradle-helpers/BuildToolTask.gradle"
35+
apply from: "gradle-helpers/CustomExecutionLogger.gradle"
36+
def outLogger = services.get(StyledTextOutputFactory).create("colouredOutputLogger")
3237

3338
def enableKotlin = (project.hasProperty("useKotlin") && project.useKotlin == "true")
3439

@@ -39,7 +44,7 @@ if (enableKotlin) {
3944

4045
def onlyX86 = project.hasProperty("onlyX86")
4146
if (onlyX86) {
42-
println "OnlyX86 build triggered."
47+
outLogger.withStyle(Style.Info).println "OnlyX86 build triggered."
4348
}
4449

4550
//common
@@ -146,15 +151,15 @@ version of the {N} CLI install a previous version of the runtime package - 'tns
146151
def pathToBuildScriptGradle = "$absolutePathToAppResources/Android/buildscript.gradle"
147152
def buildScriptGradle = file(pathToBuildScriptGradle)
148153
if (buildScriptGradle.exists()) {
149-
println "\t + applying user-defined buildscript from ${buildScriptGradle}"
154+
outLogger.withStyle(Style.SuccessHeader).println "\t + applying user-defined buildscript from ${buildScriptGradle}"
150155
apply from: pathToBuildScriptGradle, to: buildscript
151156
}
152157

153158
nativescriptDependencies.each { dep ->
154159
def pathToPluginBuildScriptGradle = "$rootDir/${dep.directory}/$PLATFORMS_ANDROID/buildscript.gradle"
155160
def pluginBuildScriptGradle = file(pathToPluginBuildScriptGradle)
156161
if (pluginBuildScriptGradle.exists()) {
157-
println "\t + applying user-defined buildscript from dependency ${pluginBuildScriptGradle}"
162+
outLogger.withStyle(Style.SuccessHeader).println "\t + applying user-defined buildscript from dependency ${pluginBuildScriptGradle}"
158163
apply from: pathToPluginBuildScriptGradle, to: buildscript
159164
}
160165
}
@@ -172,7 +177,7 @@ def applyBeforePluginGradleConfiguration = { ->
172177
def pathToBeforePluginGradle = "$appResourcesPath/Android/before-plugins.gradle"
173178
def beforePluginGradle = file(pathToBeforePluginGradle)
174179
if (beforePluginGradle.exists()) {
175-
println "\t + applying user-defined configuration from ${beforePluginGradle}"
180+
outLogger.withStyle(Style.SuccessHeader).println "\t + applying user-defined configuration from ${beforePluginGradle}"
176181
apply from: pathToBeforePluginGradle
177182
}
178183
}
@@ -182,10 +187,10 @@ def applyAppGradleConfiguration = { ->
182187
def pathToAppGradle = "$appResourcesPath/Android/app.gradle"
183188
def appGradle = file(pathToAppGradle)
184189
if (appGradle.exists()) {
185-
println "\t + applying user-defined configuration from ${appGradle}"
190+
outLogger.withStyle(Style.SuccessHeader).println "\t + applying user-defined configuration from ${appGradle}"
186191
apply from: pathToAppGradle
187192
} else {
188-
println "\t + couldn't load user-defined configuration from ${appGradle}. File doesn't exist."
193+
outLogger.withStyle(Style.Info).println "\t + couldn't load user-defined configuration from ${appGradle}. File doesn't exist."
189194
}
190195
}
191196

@@ -211,7 +216,7 @@ def getAppIdentifier = { packageJsonMap ->
211216
}
212217

213218
def setAppIdentifier = { ->
214-
println "\t + setting applicationId"
219+
outLogger.withStyle(Style.SuccessHeader).println "\t + setting applicationId"
215220
File packageJsonFile = new File("$USER_PROJECT_ROOT/$PACKAGE_JSON")
216221

217222
if (packageJsonFile.exists()) {
@@ -339,7 +344,7 @@ dependencies {
339344
androidXMaterialVersion = androidXMaterial
340345
}
341346

342-
println "\t + using android X library androidx.legacy:legacy-support-v4:$androidXLegacyVersion"
347+
outLogger.withStyle(Style.SuccessHeader).println "\t + using android X library androidx.legacy:legacy-support-v4:$androidXLegacyVersion"
343348

344349
implementation "androidx.multidex:multidex:2.0.1"
345350
implementation "androidx.legacy:legacy-support-v4:$androidXLegacyVersion"
@@ -374,7 +379,7 @@ dependencies {
374379
runtime = "nativescript-regular"
375380
}
376381

377-
println "\t + adding nativescript runtime package dependency: $runtime"
382+
outLogger.withStyle(Style.SuccessHeader).println "\t + adding nativescript runtime package dependency: $runtime"
378383
project.dependencies.add("implementation", [name: runtime, ext: "aar"])
379384
} else {
380385
implementation project(':runtime')
@@ -397,14 +402,14 @@ task addDependenciesFromNativeScriptPlugins {
397402
aarFiles.each { aarFile ->
398403
def length = aarFile.name.length() - 4
399404
def fileName = aarFile.name[0..<length]
400-
println "\t + adding aar plugin dependency: " + aarFile.getAbsolutePath()
405+
outLogger.withStyle(Style.SuccessHeader).println "\t + adding aar plugin dependency: " + aarFile.getAbsolutePath()
401406
project.dependencies.add("implementation", [name: fileName, ext: "aar"])
402407
}
403408

404409
def jarFiles = fileTree(dir: file("$rootDir/${dep.directory}/$PLATFORMS_ANDROID"), include: ["**/*.jar"])
405410
jarFiles.each { jarFile ->
406411
def jarFileAbsolutePath = jarFile.getAbsolutePath()
407-
println "\t + adding jar plugin dependency: $jarFileAbsolutePath"
412+
outLogger.withStyle(Style.SuccessHeader).println "\t + adding jar plugin dependency: $jarFileAbsolutePath"
408413
pluginsJarLibraries.add(jarFile.getAbsolutePath())
409414
}
410415

@@ -420,14 +425,14 @@ task addDependenciesFromAppResourcesLibraries {
420425
aarFiles.each { aarFile ->
421426
def length = aarFile.name.length() - 4
422427
def fileName = aarFile.name[0..<length]
423-
println "\t + adding aar library dependency: " + aarFile.getAbsolutePath()
428+
outLogger.withStyle(Style.SuccessHeader).println "\t + adding aar library dependency: " + aarFile.getAbsolutePath()
424429
project.dependencies.add("implementation", [name: fileName, ext: "aar"])
425430
}
426431

427432
def jarFiles = fileTree(dir: appResourcesLibraries, include: ["**/*.jar"])
428433
jarFiles.each { jarFile ->
429434
def jarFileAbsolutePath = jarFile.getAbsolutePath()
430-
println "\t + adding jar plugin dependency: $jarFileAbsolutePath"
435+
outLogger.withStyle(Style.SuccessHeader).println "\t + adding jar plugin dependency: $jarFileAbsolutePath"
431436
pluginsJarLibraries.add(jarFile.getAbsolutePath())
432437
}
433438

@@ -464,7 +469,7 @@ tasks.whenTaskAdded({ org.gradle.api.DefaultTask currentTask ->
464469
///////////////////////////// EXECUTUION PHASE /////////////////////////////////////
465470
////////////////////////////////////////////////////////////////////////////////////
466471

467-
task runSbg(type: JavaExec) {
472+
task runSbg(type: BuildToolTask) {
468473
dependsOn "collectAllJars"
469474
if (!findProject(':static-binding-generator').is(null)) {
470475
dependsOn ':static-binding-generator:jar'
@@ -484,6 +489,8 @@ task runSbg(type: JavaExec) {
484489
paramz.add("-show-deprecation-warnings")
485490
}
486491

492+
setOutputs outLogger
493+
487494
args paramz
488495

489496
doFirst {
@@ -576,7 +583,7 @@ afterEvaluate { project ->
576583
}
577584
}
578585
} else {
579-
println "WARNING: Folder ${jarDir.path} does not exists, the dependent project's classes won't be included in the metadata"
586+
outLogger.withStyle(Style.Info).println "WARNING: Folder ${jarDir.path} does not exists, the dependent project's classes won't be included in the metadata"
580587
}
581588
}
582589
}
@@ -695,7 +702,7 @@ task collectAllJars {
695702
}
696703
}
697704

698-
task buildMetadata(type: JavaExec) {
705+
task buildMetadata(type: BuildToolTask) {
699706
if (!findProject(':android-metadata-generator').is(null)) {
700707
dependsOn ':android-metadata-generator:jar'
701708
}
@@ -749,11 +756,13 @@ task buildMetadata(type: JavaExec) {
749756
workingDir "$BUILD_TOOLS_PATH"
750757
main "-jar"
751758

759+
setOutputs outLogger
760+
752761
args "android-metadata-generator.jar"
753762
}
754763
}
755764

756-
task generateTypescriptDefinitions(type: JavaExec) {
765+
task generateTypescriptDefinitions(type: BuildToolTask) {
757766
if (!findProject(':dts-generator').is(null)) {
758767
dependsOn ':dts-generator:jar'
759768
}
@@ -784,7 +793,10 @@ task generateTypescriptDefinitions(type: JavaExec) {
784793
new File("$TYPINGS_PATH").mkdirs()
785794

786795
logger.info("Task generateTypescriptDefinitions: Call dts-generator.jar with arguments: " + paramz.toString().replaceAll(',', ''))
787-
println "Task generateTypescriptDefinitions: Call dts-generator.jar with arguments: " + paramz.toString().replaceAll(',', '')
796+
outLogger.withStyle(Style.SuccessHeader).println "Task generateTypescriptDefinitions: Call dts-generator.jar with arguments: " + paramz.toString().replaceAll(',', '')
797+
798+
setOutputs outLogger
799+
788800
args paramz.toArray()
789801
}
790802
}
@@ -807,7 +819,7 @@ static def shouldIncludeDirForTypings(path, includeDirs) {
807819

808820
task copyTypings {
809821
doLast {
810-
println "Copied generated typings to application root level. Make sure to import android.d.ts in reference.d.ts"
822+
outLogger.withStyle(Style.Info).println "Copied generated typings to application root level. Make sure to import android.d.ts in reference.d.ts"
811823

812824
copy {
813825
from "$TYPINGS_PATH"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import static org.gradle.internal.logging.text.StyledTextOutput.Style
2+
3+
class BuildToolTask extends JavaExec {
4+
void setOutputs(def logger) {
5+
def logFile = new File("$workingDir/${name}.log")
6+
if(logFile.exists()) {
7+
logFile.delete()
8+
}
9+
standardOutput new FileOutputStream(logFile)
10+
errorOutput new FailureOutputStream(logger, logFile)
11+
}
12+
}
13+
14+
class FailureOutputStream extends OutputStream {
15+
private logger
16+
private File logFile
17+
private currentLine = ""
18+
private firstWrite = true
19+
FailureOutputStream(inLogger, inLogFile) {
20+
logger = inLogger
21+
logFile = inLogFile
22+
}
23+
24+
@Override
25+
void write(int i) throws IOException {
26+
if(firstWrite) {
27+
println ""
28+
firstWrite = false
29+
}
30+
currentLine += String.valueOf((char) i)
31+
}
32+
33+
@Override
34+
void flush() {
35+
if(currentLine?.trim()) {
36+
logger.withStyle(Style.Failure).println currentLine.trim()
37+
currentLine = ""
38+
}
39+
}
40+
41+
@Override
42+
void close() {
43+
if(!firstWrite && logFile.exists()) {
44+
logger.withStyle(Style.Info).println "Detailed log here: ${logFile.getAbsolutePath()}\n"
45+
}
46+
super.close()
47+
}
48+
}
49+
50+
ext.BuildToolTask = BuildToolTask
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import org.gradle.internal.logging.text.StyledTextOutputFactory
2+
3+
import static org.gradle.internal.logging.text.StyledTextOutput.Style
4+
def outLogger = services.get(StyledTextOutputFactory).create("colouredOutputLogger")
5+
6+
class CustomExecutionLogger extends BuildAdapter implements TaskExecutionListener {
7+
private logger
8+
private failedTask
9+
10+
CustomExecutionLogger(passedLogger) {
11+
logger = passedLogger
12+
}
13+
14+
void buildStarted(Gradle gradle) {
15+
failedTask = null
16+
}
17+
18+
void beforeExecute(Task task) {
19+
}
20+
21+
void afterExecute(Task task, TaskState state) {
22+
def failure = state.getFailure()
23+
if(failure) {
24+
failedTask = task
25+
}
26+
}
27+
28+
void buildFinished(BuildResult result) {
29+
def failure = result.getFailure()
30+
if(failure) {
31+
if(failedTask && (failedTask.getClass().getName().contains("BuildToolTask"))) {
32+
// the error from this task is already logged
33+
return
34+
}
35+
36+
println ""
37+
logger.withStyle(Style.FailureHeader).println failure.getMessage()
38+
39+
def causeException = failure.getCause()
40+
while (causeException != null) {
41+
failure = causeException
42+
causeException = failure.getCause()
43+
}
44+
if(failure != causeException) {
45+
logger.withStyle(Style.Failure).println failure.getMessage()
46+
}
47+
println ""
48+
}
49+
}
50+
}
51+
52+
gradle.useLogger(new CustomExecutionLogger(outLogger))

test-app/build-tools/android-metadata-generator/src/src/com/telerik/metadata/Generator.java

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,39 @@ public class Generator {
1919
/**
2020
* @param args
2121
*/
22-
public static void main(String[] args) throws Exception {
23-
String metadataOutputDir = "bin";
24-
List<String> params = null;
25-
22+
public static void main(String[] args) {
2623
try {
27-
metadataOutputDir = getFileRows(MDG_OUTPUT_DIR).get(0);
28-
} catch (Exception e) {
29-
throw new InvalidParameterException(String.format("You need to pass a file containing a single line: the output dir for the metadata generator1\n", e.getMessage()));
30-
}
31-
try {
32-
params = getFileRows(MDG_JAVA_DEPENDENCIES);
33-
} catch (Exception e) {
34-
throw new InvalidParameterException(String.format("You need to pass a file containing a list of jar/class paths, so metadata can be generated for them!\n", e.getMessage()));
35-
}
24+
String metadataOutputDir;
25+
List<String> params;
3626

37-
TreeNode root = Builder.build(params);
27+
try {
28+
metadataOutputDir = getFileRows(MDG_OUTPUT_DIR).get(0);
29+
} catch (Exception e) {
30+
throw new InvalidParameterException(String.format("You need to pass a file containing a single line: the output dir for the metadata generator1\n", e.getMessage()));
31+
}
32+
try {
33+
params = getFileRows(MDG_JAVA_DEPENDENCIES);
34+
} catch (Exception e) {
35+
throw new InvalidParameterException(String.format("You need to pass a file containing a list of jar/class paths, so metadata can be generated for them!\n", e.getMessage()));
36+
}
3837

39-
FileOutputStream ovs = new FileOutputStream(new File(metadataOutputDir, "treeValueStream.dat"));
40-
FileStreamWriter outValueStream = new FileStreamWriter(ovs);
38+
TreeNode root = Builder.build(params);
4139

42-
FileOutputStream ons = new FileOutputStream(new File(metadataOutputDir, "treeNodeStream.dat"));
43-
FileStreamWriter outNodeStream = new FileStreamWriter(ons);
40+
FileOutputStream ovs = new FileOutputStream(new File(metadataOutputDir, "treeValueStream.dat"));
41+
FileStreamWriter outValueStream = new FileStreamWriter(ovs);
4442

45-
FileOutputStream oss = new FileOutputStream(new File(metadataOutputDir, "treeStringsStream.dat"));
46-
FileStreamWriter outStringsStream = new FileStreamWriter(oss);
43+
FileOutputStream ons = new FileOutputStream(new File(metadataOutputDir, "treeNodeStream.dat"));
44+
FileStreamWriter outNodeStream = new FileStreamWriter(ons);
4745

48-
new Writer(outNodeStream, outValueStream, outStringsStream).writeTree(root);
46+
FileOutputStream oss = new FileOutputStream(new File(metadataOutputDir, "treeStringsStream.dat"));
47+
FileStreamWriter outStringsStream = new FileStreamWriter(oss);
48+
49+
new Writer(outNodeStream, outValueStream, outStringsStream).writeTree(root);
50+
} catch (Throwable ex) {
51+
System.err.println(String.format("Error executing Metadata Generator: %s", ex.getMessage()));
52+
ex.printStackTrace(System.out);
53+
System.exit(1);
54+
}
4955
}
5056

5157
public static List<String> getFileRows(String filename) throws IOException {

0 commit comments

Comments
 (0)