diff options
author | Hung Nguyen <hungnv@google.com> | 2020-04-15 16:44:27 +0100 |
---|---|---|
committer | Hung Nguyen <hungnv@google.com> | 2020-04-24 12:58:37 +0000 |
commit | 17f7e76cbe7a02da776e567f57aef7dc7f03fdff (patch) | |
tree | 8f2e1353b32762e1a78f127bf9b9e655c6d6c816 | |
parent | 9d9b437bf5f6ea210c4b75b0bba08b61df6015f9 (diff) | |
download | data-binding-17f7e76cbe7a02da776e567f57aef7dc7f03fdff.tar.gz |
Remove stale data binding outputs (tools/data-binding)
For each layout file containing data binding constructs, data binding
generates a corresponding layout info file and binding class files.
In an incremental build, there are 2 cases when layout info files/
binding class files get stale and need to be removed:
1. Layout files previously containing data binding constructs were
removed
2. Layout files previously containing data binding constructs are now
no longer containing them
Previously, only case #1 was handled and case #2 was missing. This CL
handles case #2.
Bug: 153711619
Test: New test in DataBindingIncrementalTest
Change-Id: I718b614222e5447503417b51d53486686c391273
-rw-r--r-- | compilerCommon/src/main/java/android/databinding/tool/LayoutXmlProcessor.java | 24 | ||||
-rw-r--r-- | compilerCommon/src/main/java/android/databinding/tool/store/ResourceBundle.java | 27 |
2 files changed, 47 insertions, 4 deletions
diff --git a/compilerCommon/src/main/java/android/databinding/tool/LayoutXmlProcessor.java b/compilerCommon/src/main/java/android/databinding/tool/LayoutXmlProcessor.java index a9fd885c..dd3cf1d5 100644 --- a/compilerCommon/src/main/java/android/databinding/tool/LayoutXmlProcessor.java +++ b/compilerCommon/src/main/java/android/databinding/tool/LayoutXmlProcessor.java @@ -150,6 +150,11 @@ public class LayoutXmlProcessor { mResourceBundle.addRemovedFile(input); } + /** Processes a layout file which does not contain data binding constructs. */ + public void processFileWithNoDataBinding(@NonNull File file) { + mResourceBundle.addFileWithNoDataBinding(file); + } + public boolean processSingleFile(@NonNull RelativizableFile input, @NonNull File output, boolean isViewBindingEnabled) throws ParserConfigurationException, SAXException, XPathExpressionException, @@ -258,13 +263,26 @@ public class LayoutXmlProcessor { } public void writeLayoutInfoFiles(File xmlOutDir, JavaFileWriter writer) throws JAXBException { + // For each layout file, generate a corresponding layout info file for (ResourceBundle.LayoutFileBundle layout : mResourceBundle .getAllLayoutFileBundlesInSource()) { writeXmlFile(writer, xmlOutDir, layout); } - for (File file : mResourceBundle.getRemovedFiles()) { - String exportFileName = generateExportFileName(file); - FileUtils.deleteQuietly(new File(xmlOutDir, exportFileName)); + + // Delete stale layout info files due to removed/changed layout files. There are 2 cases: + // 1. Layout files were removed + // 2. Layout files previously containing data binding constructs are now no longer + // containing them (see bug 153711619). NOTE: This set of layout files is a subset of + // mResourceBundle.getFilesWithNoDataBinding() because + // mResourceBundle.getFilesWithNoDataBinding() may also contain layout files that do + // not have a history or did not have data binding constructs in the previous build, in + // which cases their associated layout info files don't exist. + List<File> staleLayoutFiles = new ArrayList<>(mResourceBundle.getRemovedFiles()); + staleLayoutFiles.addAll(mResourceBundle.getFilesWithNoDataBinding()); + for (File staleLayoutFile : staleLayoutFiles) { + File staleLayoutInfoFile = new File(xmlOutDir, generateExportFileName(staleLayoutFile)); + // Delete quietly as the file may not exist (see comment above) + FileUtils.deleteQuietly(staleLayoutInfoFile); } } diff --git a/compilerCommon/src/main/java/android/databinding/tool/store/ResourceBundle.java b/compilerCommon/src/main/java/android/databinding/tool/store/ResourceBundle.java index d118fc55..7561175b 100644 --- a/compilerCommon/src/main/java/android/databinding/tool/store/ResourceBundle.java +++ b/compilerCommon/src/main/java/android/databinding/tool/store/ResourceBundle.java @@ -92,7 +92,23 @@ public class ResourceBundle implements Serializable { private Map<String, IncludedLayout> mDependencyBinders = new HashMap<>(); - private List<File> mRemovedFiles = new ArrayList<File>(); + /** + * Layout files that were removed. We track these files to delete stale layout info files that + * data binding generated in the previous build. + */ + @NonNull private List<File> mRemovedFiles = new ArrayList<File>(); + + /** + * Layout files that exist in the current build but do not contain data binding constructs. + * These include: + * 1. Layout files that previously contained data binding constructs but are now no longer + * containing them. We track these files to delete stale layout info files that data + * binding generated in the previous build (see bug 153711619). + * 2. Layout files that do not have a history or did not have data binding constructs in the + * previous build. We don't really need to track these files as there are no corresponding + * stale layout info files to delete, but it's easier (and okay) to leave them here. + */ + @NonNull private List<File> mFileWithNoDataBinding = new ArrayList<>(); private final String viewDataBindingClass; @@ -549,6 +565,15 @@ public class ResourceBundle implements Serializable { return mRemovedFiles; } + public void addFileWithNoDataBinding(@NonNull File file) { + mFileWithNoDataBinding.add(file); + } + + @NonNull + public List<File> getFilesWithNoDataBinding() { + return new ArrayList<>(mFileWithNoDataBinding); + } + @XmlAccessorType(XmlAccessType.NONE) @XmlRootElement(name = "Layout") public static class LayoutFileBundle implements Serializable, FileScopeProvider { |