private <M> void serializeMetadata(
     ScriptSource scriptSource,
     CompileOperation<M> extractingTransformer,
     File metadataDir,
     boolean emptyScript,
     boolean hasMethods) {
   File metadataFile = new File(metadataDir, METADATA_FILE_NAME);
   try {
     GFileUtils.mkdirs(metadataDir);
     KryoBackedEncoder encoder = new KryoBackedEncoder(new FileOutputStream(metadataFile));
     try {
       byte flags = (byte) ((emptyScript ? EMPTY_FLAG : 0) | (hasMethods ? HAS_METHODS_FLAG : 0));
       encoder.writeByte(flags);
       if (extractingTransformer != null && extractingTransformer.getDataSerializer() != null) {
         Serializer<M> serializer = extractingTransformer.getDataSerializer();
         serializer.write(encoder, extractingTransformer.getExtractedData());
       }
     } finally {
       encoder.close();
     }
   } catch (Exception e) {
     throw new GradleException(
         String.format(
             "Failed to serialize script metadata extracted for %s",
             scriptSource.getDisplayName()),
         e);
   }
 }
  @Override
  public void compileToDir(
      ScriptSource source,
      ClassLoader classLoader,
      File classesDir,
      File metadataDir,
      CompileOperation<?> extractingTransformer,
      Class<? extends Script> scriptBaseClass,
      Action<? super ClassNode> verifier) {
    Clock clock = new Clock();
    GFileUtils.deleteDirectory(classesDir);
    GFileUtils.mkdirs(classesDir);
    CompilerConfiguration configuration = createBaseCompilerConfiguration(scriptBaseClass);
    configuration.setTargetDirectory(classesDir);
    try {
      compileScript(
          source,
          classLoader,
          configuration,
          classesDir,
          metadataDir,
          extractingTransformer,
          verifier);
    } catch (GradleException e) {
      GFileUtils.deleteDirectory(classesDir);
      GFileUtils.deleteDirectory(metadataDir);
      throw e;
    }

    logger.debug(
        "Timing: Writing script to cache at {} took: {}",
        classesDir.getAbsolutePath(),
        clock.getTime());
  }
    public DefaultFileLock(
        File target, LockMode mode, String displayName, String operationDisplayName)
        throws Throwable {
      if (mode == LockMode.None) {
        throw new UnsupportedOperationException("Locking mode None is not supported.");
      }

      this.target = target;

      this.displayName = displayName;
      this.operationDisplayName = operationDisplayName;
      if (target.isDirectory()) {
        lockFile = new File(target, target.getName() + ".lock");
      } else {
        lockFile = new File(target.getParentFile(), target.getName() + ".lock");
      }

      GFileUtils.mkdirs(lockFile.getParentFile());
      lockFile.createNewFile();
      lockFileAccess = new RandomAccessFile(lockFile, "rw");
      try {
        lock = lock(mode);
        integrityViolated = !getUnlockedCleanly();
      } catch (Throwable t) {
        // Also releases any locks
        lockFileAccess.close();
        throw t;
      }

      this.mode = lock.isShared() ? LockMode.Shared : LockMode.Exclusive;
    }
Example #4
0
  public WorkResult execute(CommandLineToolInvocation invocation) {
    ExecAction compiler = execActionFactory.newExecAction();
    compiler.executable(executable);
    if (invocation.getWorkDirectory() != null) {
      GFileUtils.mkdirs(invocation.getWorkDirectory());
      compiler.workingDir(invocation.getWorkDirectory());
    }

    compiler.args(invocation.getArgs());

    if (!invocation.getPath().isEmpty()) {
      String pathVar = OperatingSystem.current().getPathVar();
      String compilerPath = Joiner.on(File.pathSeparator).join(invocation.getPath());
      compilerPath = compilerPath + File.pathSeparator + System.getenv(pathVar);
      compiler.environment(pathVar, compilerPath);
    }

    compiler.environment(invocation.getEnvironment());

    try {
      compiler.execute();
    } catch (ExecException e) {
      throw new GradleException(
          String.format("%s failed; see the error output for details.", action), e);
    }
    return new SimpleWorkResult(true);
  }
  public DefaultPersistentDirectoryStore open() {
    GFileUtils.mkdirs(dir);
    cacheAccess = createCacheAccess();
    try {
      cacheAccess.open(lockOptions);
    } catch (Throwable e) {
      throw new CacheOpenException(String.format("Could not open %s.", this), e);
    }

    return this;
  }
 public <E> PersistentStateCache<E> openStateCache(
     File cacheDir,
     CacheUsage usage,
     CacheValidator validator,
     Map<String, ?> properties,
     FileLockManager.LockMode lockMode,
     Serializer<E> serializer) {
   GFileUtils.mkdirs(cacheDir);
   return new SimpleStateCache<E>(
       new File(cacheDir, "state.bin"), new NoOpFileLock(), new DefaultSerializer<E>());
 }
 public PersistentCache open(
     File cacheDir,
     String displayName,
     CacheUsage usage,
     CacheValidator cacheValidator,
     Map<String, ?> properties,
     FileLockManager.LockMode lockMode,
     Action<? super PersistentCache> initializer) {
   GFileUtils.mkdirs(cacheDir);
   InMemoryCache cache = new InMemoryCache(cacheDir);
   if (initializer != null) {
     initializer.execute(cache);
   }
   return cache;
 }
  protected void transformArgs(List<String> input, List<String> output, File tempDir) {
    GFileUtils.mkdirs(tempDir);
    File optionsFile = new File(tempDir, "options.txt");
    try {
      PrintWriter writer = new PrintWriter(optionsFile);
      try {
        ArgWriter argWriter = argWriterFactory.transform(writer);
        argWriter.args(input);
      } finally {
        IOUtils.closeQuietly(writer);
      }
    } catch (IOException e) {
      throw new UncheckedIOException(
          String.format(
              "Could not write compiler options file '%s'.", optionsFile.getAbsolutePath()),
          e);
    }

    output.add(String.format("@%s", optionsFile.getAbsolutePath()));
  }
  private DaemonStartupInfo startProcess(List<String> args, File workingDir, InputStream stdInput) {
    LOGGER.info("Starting daemon process: workingDir = {}, daemonArgs: {}", workingDir, args);
    Clock clock = new Clock();
    try {
      GFileUtils.mkdirs(workingDir);

      DaemonOutputConsumer outputConsumer = new DaemonOutputConsumer(stdInput);
      ExecHandle handle = new DaemonExecHandleBuilder().build(args, workingDir, outputConsumer);

      handle.start();
      LOGGER.debug("Gradle daemon process is starting. Waiting for the daemon to detach...");
      handle.waitForFinish();
      LOGGER.debug("Gradle daemon process is now detached.");

      return daemonGreeter.parseDaemonOutput(outputConsumer.getProcessOutput());
    } catch (GradleException e) {
      throw e;
    } catch (Exception e) {
      throw new GradleException("Could not start Gradle daemon.", e);
    } finally {
      LOGGER.info("An attempt to start the daemon took {}.", clock.getTime());
    }
  }