package com.github.sherter.googlejavaformatgradleplugin;

import dagger.Module;
import dagger.Provides;
import org.gradle.api.Project;
import org.gradle.api.logging.Logger;

import javax.inject.Named;
import java.nio.file.Path;
import java.nio.file.Paths;

@Module
class PersistenceModule {

  private final Project project;

  PersistenceModule(Project project) {
    this.project = project;
  }

  @Provides
  Logger provideLogger() {
    return project.getLogger();
  }

  @Provides
  @Named("plugin version")
  String providePluginVersion() {
    // TODO(sherter): compute this dynamically
    return "0.3";
  }

  @Provides
  @Named("base path")
  Path provideBasePath() {
    return project.getProjectDir().toPath();
  }

  @Provides
  @Named("output path")
  Path provideOutputPath(@Named("plugin version") String pluginVersion) {
    Path buildDir = project.getBuildDir().toPath();
    return buildDir.resolve(Paths.get("google-java-format", pluginVersion));
  }

  @Provides
  @Named("storage")
  Path provideStoragePath(@Named("output path") Path outputPath) {
    return outputPath.resolve("fileStates.txt");
  }

  @Provides
  @Named("settings")
  Path provideSettingsPath(@Named("output path") Path outputPath) {
    return outputPath.resolve("settings.txt");
  }
}

@@ -51,16 +51,99 @@
 lugin;%0A%0A
+import com.google.common.base.Throwables;%0Aimport com.google.common.io.CharStreams;%0A
 import d
@@ -156,16 +156,16 @@
 Module;%0A
-
 import d
@@ -277,16 +277,148 @@
 .Named;%0A
+import java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.InputStreamReader;%0Aimport java.nio.charset.StandardCharsets;%0A
 import j
@@ -763,68 +763,234 @@
     
-// TODO(sherter): compute this dynamically%0A    return %220.3%22;
+try (InputStream is = getClass().getResourceAsStream(%22/VERSION%22)) %7B%0A      return CharStreams.toString(new InputStreamReader(is, StandardCharsets.UTF_8));%0A    %7D catch (IOException e) %7B%0A      throw Throwables.propagate(e);%0A    %7D
 %0A  %7D

Read plugin version from VERSION file