InstantRunSlicer extends Transform { @Override public void transform(@NonNull TransformInvocation transformInvocation) throws IOException, TransformException, InterruptedException { TransformOutputProvider outputProvider = transformInvocation.getOutputProvider(); boolean isIncremental = transformInvocation.isIncremental(); Collection<TransformInput> inputs = transformInvocation.getInputs(); if (outputProvider == null) { logger.error(null , "null TransformOutputProvider for InstantRunSlicer"); return; } Slices slices = new Slices(); if (isIncremental) { sliceIncrementally(inputs, outputProvider, slices); } else { slice(inputs, outputProvider, slices); combineAllJars(inputs, outputProvider); } } InstantRunSlicer(@NonNull Logger logger, @NonNull InstantRunVariantScope variantScope); @NonNull @Override String getName(); @NonNull @Override Set<QualifiedContent.ContentType> getInputTypes(); @NonNull @Override Set<QualifiedContent.ContentType> getOutputTypes(); @NonNull @Override Set<Scope> getScopes(); @Override boolean isIncremental(); @NonNull @Override Map<String, Object> getParameterInputs(); @Override void transform(@NonNull TransformInvocation transformInvocation); static final String MAIN_SLICE_NAME; static final int NUMBER_OF_SLICES_FOR_PROJECT_CLASSES; }
@Test public void testAddingDirectory() throws IOException, TransformException, InterruptedException { InstantRunSlicer slicer = new InstantRunSlicer(logger, variantScope); String packagePath = "com/foo/bar"; File folder = new File(getInputDir(), packagePath); File singleClassFile = createFile(folder, "file0.class"); Map<File, Status> changedFiles = ImmutableMap.of(folder, Status.ADDED); slicer.transform(new TransformInvocationBuilder(context) .addInputs(ImmutableList.of(getInput(getInputDir(), changedFiles))) .addOutputProvider(getOutputProvider(getOutputDir(), jarOutput)) .build()); File[] outputSlices = getOutputDir().listFiles(); assertThat(outputSlices).isNotNull(); assertThat(outputSlices).hasLength(InstantRunSlicer.NUMBER_OF_SLICES_FOR_PROJECT_CLASSES); Optional<Integer> sliceForFile = findSliceForFile(getOutputDir(), getInputDir(), singleClassFile); assertThat(sliceForFile.isPresent()); int slot = sliceForFile.get(); File outputSlice = outputSlices[slot]; File outputFile = new File(new File(outputSlice, packagePath), "file0.class"); assertThat(outputFile.exists()).isTrue(); }
@Test public void testRemovingDirectory() throws IOException, TransformException, InterruptedException { InstantRunSlicer slicer = new InstantRunSlicer(logger, variantScope); String packagePath = "com/foo/bar"; File folder = new File(getInputDir(), packagePath); File singleClassFile = createFile(folder, "file0.class"); Map<File, Status> changedFiles = ImmutableMap.of(folder, Status.ADDED); slicer.transform(new TransformInvocationBuilder(context) .addInputs(ImmutableList.of(getInput(getInputDir(), changedFiles))) .addOutputProvider(getOutputProvider(getOutputDir(), jarOutput)) .build()); FileUtils.deleteDirectoryContents(folder); assertTrue(folder.delete()); changedFiles = ImmutableMap.of(folder, Status.REMOVED); slicer.transform(new TransformInvocationBuilder(context) .addInputs(ImmutableList.of(getInput(getInputDir(), changedFiles))) .addOutputProvider(getOutputProvider(getOutputDir(), jarOutput)) .build()); File[] outputSlices = getOutputDir().listFiles(); assertThat(outputSlices).isNotNull(); assertThat(outputSlices).hasLength(InstantRunSlicer.NUMBER_OF_SLICES_FOR_PROJECT_CLASSES); Optional<Integer> sliceForFile = findSliceForFile(getOutputDir(), getInputDir(), singleClassFile); assertThat(!sliceForFile.isPresent()); }