Project

Profile

Help

Revision a8341bd5

Added by Norman Tovey-Walsh 6 months ago

Automatically clean up build sources when switching branches

View differences:

build.gradle
142 142
  pathSeparator = System.getProperty("file.separator")
143 143
}
144 144

  
145
public class DeleteFilesRecursively {
146
  static void delete(File file){
147
    for (File fspec : file.listFiles()) {
148
      if (fspec.isDirectory()) {
149
        delete(fspec);
150
      } else {
151
        fspec.delete();
152
      }
153
    }
154
    file.delete();
155
  }
156
}
157

  
158
// ============================================================
159
// Have you switched branches?
160
def branch = new ByteArrayOutputStream()
161
def result = exec {
162
  commandLine "git", "branch", "--show-current"
163
  standardOutput = branch;
164
}
165
def currentBranch = branch.toString().trim()
166

  
167
def builtBranch = "none";
168
try {
169
  def xbranch = new BufferedReader(new FileReader(new File("build/built-branch")))
170
  builtBranch = "${xbranch.readLine().trim()}"
171
} catch (FileNotFoundException foe) {
172
  // nop
173
}
174

  
175
if (currentBranch != builtBranch
176
    && (file("${buildDir}/src").exists()
177
        || file("${buildDir}/preprocess").exists()
178
        || file("${buildDir}/filtered").exists()
179
        || file("${buildDir}/classes").exists())) {
180
  println("You appear to have switched to the '${currentBranch}' branch.")
181
  println("Deleting stale build sources...")
182
  DeleteFilesRecursively.delete(new File("${buildDir}/src"))
183
  DeleteFilesRecursively.delete(new File("${buildDir}/preprocess"))
184
  DeleteFilesRecursively.delete(new File("${buildDir}/filtered"))
185
  DeleteFilesRecursively.delete(new File("${buildDir}/classes"))
186
}
187

  
145 188
// ============================================================
146 189
// Find jar files that need to be part of the build or release
147 190
//
......
459 502

  
460 503
task hej_setupPreprocess(
461 504
  type: Copy,
462
  dependsOn: ["hej_featureKeys"],
505
  dependsOn: ["hej_featureKeys", "record_branch"],
463 506
  description: "Copy HE sources for preprocessing HE"
464 507
) {
465 508
  from "${projectDir}/src/main/java"
......
526 569
}
527 570

  
528 571
task pej_setupPreprocess(
529
  dependsOn: ["hej_setupPreprocessForPej", "eej_setupPreprocessForPej"],
572
  dependsOn: ["hej_setupPreprocessForPej", "eej_setupPreprocessForPej", "record_branch"],
530 573
  description: "Copy sources for preprocessing PE"
531 574
) {
532 575
  // Just a container to hang dependencies on
......
534 577

  
535 578
task hej_setupPreprocessForEej(
536 579
  type: Copy,
537
  dependsOn: ["hej_featureKeys"],
580
  dependsOn: ["hej_featureKeys", "record_branch"],
538 581
  description: "Copy HE sources for EE build"
539 582
) {
540 583
  from "${projectDir}/src/main/java"
......
568 611
// N.B. bytecode is explicitly excluded, see eej_setupPreprocessBytecode
569 612
task eej_setupPreprocess(
570 613
  type: Copy,
571
  dependsOn: ["hej_setupPreprocessForEej", "eej_setupPreprocessBytecode"],
614
  dependsOn: ["hej_setupPreprocessForEej", "eej_setupPreprocessBytecode", "record_branch"],
572 615
  description: "Copy sources for preprocessing EE"
573 616
) {
574 617
  from "${projectDir}/src/main/java"
......
686 729

  
687 730
// ============================================================
688 731

  
732
task record_branch(
733
  type: Exec,
734
  description: "Records the branch so we can detect switches."
735
) {
736
  def recordBranch = new ByteArrayOutputStream();
737
  commandLine "git", "branch", "--show-current"
738
  standardOutput = recordBranch
739
  doLast {
740
    def output = new FileWriter(new File("build/built-branch"))
741
    output.write(recordBranch.toString("utf-8"))
742
    output.close()
743
  }
744
}
745

  
689 746
task hejCompile(
690 747
  type: JavaCompile,
691 748
  dependsOn: ["hej_sources"],

Also available in: Unified diff