@@ -53,46 +53,75 @@ publishing {
53
53
tasks. withType(JavaCompile ) {
54
54
options. encoding = ' UTF-8'
55
55
}
56
+ // Add these imports at the top of your build.gradle file
57
+ import java.nio.file.Files
58
+ import java.nio.file.Path
59
+ import java.nio.file.Paths
60
+ import java.nio.file.StandardCopyOption
56
61
57
- def originalFile = null ;
62
+ // Your existing build configurations...
58
63
59
- task backupCode {
60
- def file = file( ' src/main/java/org/json/JSONParserConfiguration.java ' )
61
- originalFile = file . text
62
- }
64
+ // Add a new task to modify the file
65
+ task modifyStrictMode {
66
+ doLast {
67
+ println " Modifying JSONParserConfiguration.java to enable strictMode... "
63
68
64
- task firstTest {
69
+ def filePath = project . file( ' src/main/java/org/json/JSONParserConfiguration.java ' )
65
70
66
- }
71
+ if (! filePath. exists()) {
72
+ throw new GradleException (" Could not find file: ${ filePath.absolutePath} " )
73
+ }
67
74
68
- task modifyCode {
69
- doLast {
70
- // Add your code modification logic here
71
- def file = file(' src/main/java/org/json/JSONParserConfiguration.java' )
72
- def text = file. text
73
- text = text. replaceAll(' oldCode' , ' newCode' )
74
- file. text = text
75
+ // Create a backup of the original file
76
+ def backupFile = new File (filePath. absolutePath + ' .bak' )
77
+ Files . copy(filePath. toPath(), backupFile. toPath(), StandardCopyOption . REPLACE_EXISTING )
78
+
79
+ // Read and modify the file content
80
+ def content = filePath. text
81
+ def modifiedContent = content. replace(' // this.strictMode = true;' , ' this.strictMode = true;' )
82
+
83
+ // Write the modified content back to the file
84
+ filePath. text = modifiedContent
85
+
86
+ println " File modified successfully at: ${ filePath.absolutePath} "
75
87
}
76
88
}
77
89
78
- task compileModifiedCode (type : JavaCompile ) {
79
- source = sourceSets. main. java. srcDirs
80
- classpath = sourceSets. main. compileClasspath
81
- destinationDirectory = sourceSets. main. java. outputDir
82
- }
90
+ // Add a task to restore the original file
91
+ task restoreStrictMode {
92
+ doLast {
93
+ println " Restoring original JSONParserConfiguration.java..."
83
94
84
- task secondTest {
95
+ def filePath = project. file(' src/main/java/org/json/JSONParserConfiguration.java' )
96
+ def backupFile = new File (filePath. absolutePath + ' .bak' )
85
97
98
+ if (backupFile. exists()) {
99
+ Files . copy(backupFile. toPath(), filePath. toPath(), StandardCopyOption . REPLACE_EXISTING )
100
+ backupFile. delete()
101
+ println " Original file restored successfully at: ${ filePath.absolutePath} "
102
+ } else {
103
+ println " Backup file not found at: ${ backupFile.absolutePath} . No restoration performed."
104
+ }
105
+ }
86
106
}
87
107
88
- task restoreCode {
89
- def file = file( ' src/main/java/org/json/JSONParserConfiguration.java ' )
90
- file . text = originalFile
91
- }
108
+ // Create a task to run the workflow
109
+ task testWithStrictMode {
110
+ dependsOn modifyStrictMode
111
+ finalizedBy restoreStrictMode
92
112
93
- // and then add it to the task list
94
- backupCode. finalizedBy firstTest
95
- firstTest. finalizedBy modifyCode
96
- modifyCode. finalizedBy compileModifiedCode
97
- compileModifiedCode. finalizedBy secondTest
98
- secondTest. finalizedBy restoreCode
113
+ doLast {
114
+ // This will trigger a clean build and run tests with strictMode enabled
115
+ if (org.gradle.internal.os.OperatingSystem . current(). isWindows()) {
116
+ exec {
117
+ executable ' cmd'
118
+ args ' /c' , ' gradlew.bat' , ' clean' , ' build'
119
+ }
120
+ } else {
121
+ exec {
122
+ executable ' ./gradlew'
123
+ args ' clean' , ' build'
124
+ }
125
+ }
126
+ }
127
+ }
0 commit comments