{"action":"created","comment":{"url":"https://api.github.com/repos/scalameta/scalafmt/pulls/comments/156219566","pull_request_review_id":82661870,"id":156219566,"diff_hunk":"@@ -1,25 +1,194 @@\n package org.scalafmt.sbt\n+\n+import org.scalafmt.config.{Config, ScalafmtConfig}\n+import org.scalafmt.{Formatted, Scalafmt}\n+import sbt.Keys._\n+import sbt.{Def, _}\n+import complete.DefaultParsers._\n+import metaconfig.Configured\n+import sbt.util.Logger\n+\n import scala.meta.internal.tokenizers.PlatformTokenizerCache\n-import sbt._\n-import Keys._\n+import scala.util.{Failure, Success, Try}\n \n object ScalafmtPlugin extends AutoPlugin {\n   override def trigger: PluginTrigger = allRequirements\n+\n   object autoImport {\n-    val scalafmt: Command =\n-      Command.args(\"scalafmt\", \"run the scalafmt command line interface.\") {\n-        case (state, args) =>\n-          org.scalafmt.cli.Cli.main(\"--non-interactive\" +: args.toArray)\n-          PlatformTokenizerCache.megaCache.clear()\n-          state\n+    val scalafmt = taskKey[Unit](\"Format Scala sources.\")\n+    val scalafmtCheck =\n+      taskKey[Boolean](\n+        \"Fails if a Scala source is mis-formatted. Does not write to files.\")\n+    val scalafmtOnCompile =\n+      settingKey[Boolean](\n+        \"Format Scala source files on compile, off by default.\")\n+    val scalafmtConfig = taskKey[Option[File]](\n+      \"Optional location of .scalafmt.conf file. If None the default config is used.\")\n+    val scalafmtSbt = taskKey[Unit](\n+      \"Format *.sbt and project/*.scala files for this sbt build.\")\n+    val scalafmtSbtCheck =\n+      taskKey[Boolean](\n+        \"Fails if a *.sbt or project/*.scala source is mis-formatted. Does not write to files.\")\n+    val scalafmtOnly = inputKey[Unit](\"Format only given file.\")\n+  }\n+  import autoImport._\n+\n+  private val scalafmtDoFormatOnCompile =\n+    taskKey[Unit](\"Format Scala source files if scalafmtOnCompile is on.\")\n+\n+  private val scalaConfig =\n+    scalafmtConfig.map(_.map(Config.fromHoconFile(_) match {\n+      case Configured.Ok(conf) => conf\n+      case Configured.NotOk(e) => throw new MessageOnlyException(e.msg)\n+    }).getOrElse(ScalafmtConfig.default))\n+  private val sbtConfig = scalaConfig.map(_.forSbt)\n+\n+  private type Input = String\n+  private type Output = String\n+\n+  private def withFormattedSources[T](\n+      sources: Seq[File],\n+      config: ScalafmtConfig\n+  )(\n+      onError: (File, Throwable) => T,\n+      onFormat: (File, Input, Output) => T\n+  ): Seq[Option[T]] = {\n+    sources.map(\n+      file => {\n+        val input = IO.read(file)\n+        val output = Scalafmt.format(input, config)\n+\n+        output match {\n+          case Formatted.Failure(e) =>\n+            if (config.runner.fatalWarnings) {\n+              throw e\n+            } else if (config.runner.ignoreWarnings) {\n+              // do nothing\n+              None\n+            } else {\n+              Some(onError(file, e))\n+            }\n+          case Formatted.Success(code) =>\n+            Some(onFormat(file, input, code))\n+        }\n+      }\n+    )\n+  }\n+\n+  private def formatSources(\n+      sources: Seq[File],\n+      config: ScalafmtConfig,\n+      log: Logger\n+  ): Unit = {\n+    withFormattedSources(sources, config)(\n+      (file, e) => log.error(s\"Error in ${file.toString}: $e\"),\n+      (file, input, output) => {\n+        if (input != output) {\n+          log.info(s\"Successfully formatted ${file.toString}.\")\n+          IO.write(file, output)\n+        }\n+      }\n+    )\n+    PlatformTokenizerCache.megaCache.clear()\n+  }\n+\n+  private def checkSources(\n+      sources: Seq[File],\n+      config: ScalafmtConfig,\n+      log: Logger\n+  ): Boolean = {\n+    val res = withFormattedSources(sources, config)(\n+      (file, e) => {\n+        log.error(s\"Error in ${file.toString}: $e\")\n+        false\n+      },\n+      (file, input, output) => {\n+        val diff = input != output\n+        if (diff) {\n+          throw new MessageOnlyException(\n+            s\"${file.toString} isn't formatted properly!\")\n+        }\n+        diff\n       }\n+    ).flatten.forall(x => x)\n+    PlatformTokenizerCache.megaCache.clear()\n+    res\n   }\n-  override def globalSettings: Seq[Def.Setting[_]] =\n-    Seq(\n-      commands += autoImport.scalafmt\n-    ) ++\n-      addCommandAlias(\"scalafmtTest\", \"scalafmt --test\") ++\n-      addCommandAlias(\"scalafmtDiffTest\", \"scalafmt --diff --test\") ++\n-      addCommandAlias(\"scalafmtDiff\", \"scalafmt --diff\")\n \n+  private lazy val sbtSources = thisProject.map(\n+    proj => {\n+      val rootSbt =\n+        BuildPaths.configurationSources(proj.base).filterNot(_.isHidden)\n+      val projectSbt =\n+        (BuildPaths.projectStandard(proj.base) * GlobFilter(\"*.sbt\")).get\n+          .filterNot(_.isHidden)\n+      rootSbt ++ projectSbt\n+    }\n+  )\n+  private lazy val projectSources = thisProject.map(proj =>\n+    (BuildPaths.projectStandard(proj.base) * GlobFilter(\"*.scala\")).get)\n+\n+  lazy val scalafmtConfigSettings: Seq[Def.Setting[_]] = Seq(\n+    scalafmt := formatSources(\n+      (unmanagedSources in scalafmt).value,\n+      scalaConfig.value,\n+      streams.value.log),\n+    scalafmtSbt := {\n+      formatSources(sbtSources.value, sbtConfig.value, streams.value.log)\n+      formatSources(projectSources.value, scalaConfig.value, streams.value.log)\n+    },\n+    scalafmtCheck :=\n+      checkSources(\n+        (unmanagedSources in scalafmt).value,\n+        scalaConfig.value,\n+        streams.value.log),\n+    scalafmtSbtCheck := {\n+      checkSources(sbtSources.value, sbtConfig.value, streams.value.log)\n+      checkSources(projectSources.value, scalaConfig.value, streams.value.log)\n+    },\n+    scalafmtDoFormatOnCompile := Def.settingDyn {\n+      if (scalafmtOnCompile.value) {\n+        scalafmt in resolvedScoped.value.scope","path":"scalafmt-sbt/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala","position":166,"original_position":166,"commit_id":"7b52844607a2c6128c449a22745e80e47ec9bf71","original_commit_id":"7b52844607a2c6128c449a22745e80e47ec9bf71","user":{"login":"vovapolu","id":4629312,"avatar_url":"https://avatars3.githubusercontent.com/u/4629312?v=4","gravatar_id":"","url":"https://api.github.com/users/vovapolu","html_url":"https://github.com/vovapolu","followers_url":"https://api.github.com/users/vovapolu/followers","following_url":"https://api.github.com/users/vovapolu/following{/other_user}","gists_url":"https://api.github.com/users/vovapolu/gists{/gist_id}","starred_url":"https://api.github.com/users/vovapolu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vovapolu/subscriptions","organizations_url":"https://api.github.com/users/vovapolu/orgs","repos_url":"https://api.github.com/users/vovapolu/repos","events_url":"https://api.github.com/users/vovapolu/events{/privacy}","received_events_url":"https://api.github.com/users/vovapolu/received_events","type":"User","site_admin":false},"body":"Let's leave caching for next PRs. I would like to implement it entirely, not only in some specific places. I will write a warning in the task description for now :)","created_at":"2017-12-11T22:30:23Z","updated_at":"2017-12-11T22:30:24Z","html_url":"https://github.com/scalameta/scalafmt/pull/1085#discussion_r156219566","pull_request_url":"https://api.github.com/repos/scalameta/scalafmt/pulls/1085","author_association":"COLLABORATOR","_links":{"self":{"href":"https://api.github.com/repos/scalameta/scalafmt/pulls/comments/156219566"},"html":{"href":"https://github.com/scalameta/scalafmt/pull/1085#discussion_r156219566"},"pull_request":{"href":"https://api.github.com/repos/scalameta/scalafmt/pulls/1085"}},"in_reply_to_id":154956773},"pull_request":{"url":"https://api.github.com/repos/scalameta/scalafmt/pulls/1085","id":155593157,"html_url":"https://github.com/scalameta/scalafmt/pull/1085","diff_url":"https://github.com/scalameta/scalafmt/pull/1085.diff","patch_url":"https://github.com/scalameta/scalafmt/pull/1085.patch","issue_url":"https://api.github.com/repos/scalameta/scalafmt/issues/1085","number":1085,"state":"open","locked":false,"title":"New scalafmt sbt plugin","user":{"login":"vovapolu","id":4629312,"avatar_url":"https://avatars3.githubusercontent.com/u/4629312?v=4","gravatar_id":"","url":"https://api.github.com/users/vovapolu","html_url":"https://github.com/vovapolu","followers_url":"https://api.github.com/users/vovapolu/followers","following_url":"https://api.github.com/users/vovapolu/following{/other_user}","gists_url":"https://api.github.com/users/vovapolu/gists{/gist_id}","starred_url":"https://api.github.com/users/vovapolu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vovapolu/subscriptions","organizations_url":"https://api.github.com/users/vovapolu/orgs","repos_url":"https://api.github.com/users/vovapolu/repos","events_url":"https://api.github.com/users/vovapolu/events{/privacy}","received_events_url":"https://api.github.com/users/vovapolu/received_events","type":"User","site_admin":false},"body":"https://github.com/scalameta/scalafmt/issues/1081\r\n\r\nI renamed old plugin to `sbt-cli-scalafmt`, because it just calls cli interface (and because I couldn't come up with new name for my plugin :D). I also added new scripted tests in addition to old ones. \r\n\r\nPlugin doesn't perform any caching, it just looks to `unmanagedSources` or sbt files and formats them. `*Check` tasks throw exceptions and print error messages when check is failed. ","created_at":"2017-11-30T12:19:19Z","updated_at":"2017-12-11T22:30:24Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b1f0c880c66515d77661e400f877d21b75a50c3b","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/scalameta/scalafmt/pulls/1085/commits","review_comments_url":"https://api.github.com/repos/scalameta/scalafmt/pulls/1085/comments","review_comment_url":"https://api.github.com/repos/scalameta/scalafmt/pulls/comments{/number}","comments_url":"https://api.github.com/repos/scalameta/scalafmt/issues/1085/comments","statuses_url":"https://api.github.com/repos/scalameta/scalafmt/statuses/7b52844607a2c6128c449a22745e80e47ec9bf71","head":{"label":"vovapolu:master","ref":"master","sha":"7b52844607a2c6128c449a22745e80e47ec9bf71","user":{"login":"vovapolu","id":4629312,"avatar_url":"https://avatars3.githubusercontent.com/u/4629312?v=4","gravatar_id":"","url":"https://api.github.com/users/vovapolu","html_url":"https://github.com/vovapolu","followers_url":"https://api.github.com/users/vovapolu/followers","following_url":"https://api.github.com/users/vovapolu/following{/other_user}","gists_url":"https://api.github.com/users/vovapolu/gists{/gist_id}","starred_url":"https://api.github.com/users/vovapolu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vovapolu/subscriptions","organizations_url":"https://api.github.com/users/vovapolu/orgs","repos_url":"https://api.github.com/users/vovapolu/repos","events_url":"https://api.github.com/users/vovapolu/events{/privacy}","received_events_url":"https://api.github.com/users/vovapolu/received_events","type":"User","site_admin":false},"repo":{"id":98414585,"name":"scalafmt","full_name":"vovapolu/scalafmt","owner":{"login":"vovapolu","id":4629312,"avatar_url":"https://avatars3.githubusercontent.com/u/4629312?v=4","gravatar_id":"","url":"https://api.github.com/users/vovapolu","html_url":"https://github.com/vovapolu","followers_url":"https://api.github.com/users/vovapolu/followers","following_url":"https://api.github.com/users/vovapolu/following{/other_user}","gists_url":"https://api.github.com/users/vovapolu/gists{/gist_id}","starred_url":"https://api.github.com/users/vovapolu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vovapolu/subscriptions","organizations_url":"https://api.github.com/users/vovapolu/orgs","repos_url":"https://api.github.com/users/vovapolu/repos","events_url":"https://api.github.com/users/vovapolu/events{/privacy}","received_events_url":"https://api.github.com/users/vovapolu/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/vovapolu/scalafmt","description":"Code formatter for Scala","fork":true,"url":"https://api.github.com/repos/vovapolu/scalafmt","forks_url":"https://api.github.com/repos/vovapolu/scalafmt/forks","keys_url":"https://api.github.com/repos/vovapolu/scalafmt/keys{/key_id}","collaborators_url":"https://api.github.com/repos/vovapolu/scalafmt/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/vovapolu/scalafmt/teams","hooks_url":"https://api.github.com/repos/vovapolu/scalafmt/hooks","issue_events_url":"https://api.github.com/repos/vovapolu/scalafmt/issues/events{/number}","events_url":"https://api.github.com/repos/vovapolu/scalafmt/events","assignees_url":"https://api.github.com/repos/vovapolu/scalafmt/assignees{/user}","branches_url":"https://api.github.com/repos/vovapolu/scalafmt/branches{/branch}","tags_url":"https://api.github.com/repos/vovapolu/scalafmt/tags","blobs_url":"https://api.github.com/repos/vovapolu/scalafmt/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/vovapolu/scalafmt/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/vovapolu/scalafmt/git/refs{/sha}","trees_url":"https://api.github.com/repos/vovapolu/scalafmt/git/trees{/sha}","statuses_url":"https://api.github.com/repos/vovapolu/scalafmt/statuses/{sha}","languages_url":"https://api.github.com/repos/vovapolu/scalafmt/languages","stargazers_url":"https://api.github.com/repos/vovapolu/scalafmt/stargazers","contributors_url":"https://api.github.com/repos/vovapolu/scalafmt/contributors","subscribers_url":"https://api.github.com/repos/vovapolu/scalafmt/subscribers","subscription_url":"https://api.github.com/repos/vovapolu/scalafmt/subscription","commits_url":"https://api.github.com/repos/vovapolu/scalafmt/commits{/sha}","git_commits_url":"https://api.github.com/repos/vovapolu/scalafmt/git/commits{/sha}","comments_url":"https://api.github.com/repos/vovapolu/scalafmt/comments{/number}","issue_comment_url":"https://api.github.com/repos/vovapolu/scalafmt/issues/comments{/number}","contents_url":"https://api.github.com/repos/vovapolu/scalafmt/contents/{+path}","compare_url":"https://api.github.com/repos/vovapolu/scalafmt/compare/{base}...{head}","merges_url":"https://api.github.com/repos/vovapolu/scalafmt/merges","archive_url":"https://api.github.com/repos/vovapolu/scalafmt/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/vovapolu/scalafmt/downloads","issues_url":"https://api.github.com/repos/vovapolu/scalafmt/issues{/number}","pulls_url":"https://api.github.com/repos/vovapolu/scalafmt/pulls{/number}","milestones_url":"https://api.github.com/repos/vovapolu/scalafmt/milestones{/number}","notifications_url":"https://api.github.com/repos/vovapolu/scalafmt/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/vovapolu/scalafmt/labels{/name}","releases_url":"https://api.github.com/repos/vovapolu/scalafmt/releases{/id}","deployments_url":"https://api.github.com/repos/vovapolu/scalafmt/deployments","created_at":"2017-07-26T11:17:06Z","updated_at":"2017-07-26T11:17:08Z","pushed_at":"2017-12-03T17:40:42Z","git_url":"git://github.com/vovapolu/scalafmt.git","ssh_url":"git@github.com:vovapolu/scalafmt.git","clone_url":"https://github.com/vovapolu/scalafmt.git","svn_url":"https://github.com/vovapolu/scalafmt","homepage":"http://scalameta.org/scalafmt","size":3983,"stargazers_count":0,"watchers_count":0,"language":"Scala","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":null,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"scalameta:master","ref":"master","sha":"49815ab491ff214363488072ca07ba6a5a524e96","user":{"login":"scalameta","id":7526438,"avatar_url":"https://avatars1.githubusercontent.com/u/7526438?v=4","gravatar_id":"","url":"https://api.github.com/users/scalameta","html_url":"https://github.com/scalameta","followers_url":"https://api.github.com/users/scalameta/followers","following_url":"https://api.github.com/users/scalameta/following{/other_user}","gists_url":"https://api.github.com/users/scalameta/gists{/gist_id}","starred_url":"https://api.github.com/users/scalameta/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scalameta/subscriptions","organizations_url":"https://api.github.com/users/scalameta/orgs","repos_url":"https://api.github.com/users/scalameta/repos","events_url":"https://api.github.com/users/scalameta/events{/privacy}","received_events_url":"https://api.github.com/users/scalameta/received_events","type":"Organization","site_admin":false},"repo":{"id":45423600,"name":"scalafmt","full_name":"scalameta/scalafmt","owner":{"login":"scalameta","id":7526438,"avatar_url":"https://avatars1.githubusercontent.com/u/7526438?v=4","gravatar_id":"","url":"https://api.github.com/users/scalameta","html_url":"https://github.com/scalameta","followers_url":"https://api.github.com/users/scalameta/followers","following_url":"https://api.github.com/users/scalameta/following{/other_user}","gists_url":"https://api.github.com/users/scalameta/gists{/gist_id}","starred_url":"https://api.github.com/users/scalameta/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scalameta/subscriptions","organizations_url":"https://api.github.com/users/scalameta/orgs","repos_url":"https://api.github.com/users/scalameta/repos","events_url":"https://api.github.com/users/scalameta/events{/privacy}","received_events_url":"https://api.github.com/users/scalameta/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/scalameta/scalafmt","description":"Code formatter for Scala","fork":false,"url":"https://api.github.com/repos/scalameta/scalafmt","forks_url":"https://api.github.com/repos/scalameta/scalafmt/forks","keys_url":"https://api.github.com/repos/scalameta/scalafmt/keys{/key_id}","collaborators_url":"https://api.github.com/repos/scalameta/scalafmt/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/scalameta/scalafmt/teams","hooks_url":"https://api.github.com/repos/scalameta/scalafmt/hooks","issue_events_url":"https://api.github.com/repos/scalameta/scalafmt/issues/events{/number}","events_url":"https://api.github.com/repos/scalameta/scalafmt/events","assignees_url":"https://api.github.com/repos/scalameta/scalafmt/assignees{/user}","branches_url":"https://api.github.com/repos/scalameta/scalafmt/branches{/branch}","tags_url":"https://api.github.com/repos/scalameta/scalafmt/tags","blobs_url":"https://api.github.com/repos/scalameta/scalafmt/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/scalameta/scalafmt/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/scalameta/scalafmt/git/refs{/sha}","trees_url":"https://api.github.com/repos/scalameta/scalafmt/git/trees{/sha}","statuses_url":"https://api.github.com/repos/scalameta/scalafmt/statuses/{sha}","languages_url":"https://api.github.com/repos/scalameta/scalafmt/languages","stargazers_url":"https://api.github.com/repos/scalameta/scalafmt/stargazers","contributors_url":"https://api.github.com/repos/scalameta/scalafmt/contributors","subscribers_url":"https://api.github.com/repos/scalameta/scalafmt/subscribers","subscription_url":"https://api.github.com/repos/scalameta/scalafmt/subscription","commits_url":"https://api.github.com/repos/scalameta/scalafmt/commits{/sha}","git_commits_url":"https://api.github.com/repos/scalameta/scalafmt/git/commits{/sha}","comments_url":"https://api.github.com/repos/scalameta/scalafmt/comments{/number}","issue_comment_url":"https://api.github.com/repos/scalameta/scalafmt/issues/comments{/number}","contents_url":"https://api.github.com/repos/scalameta/scalafmt/contents/{+path}","compare_url":"https://api.github.com/repos/scalameta/scalafmt/compare/{base}...{head}","merges_url":"https://api.github.com/repos/scalameta/scalafmt/merges","archive_url":"https://api.github.com/repos/scalameta/scalafmt/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/scalameta/scalafmt/downloads","issues_url":"https://api.github.com/repos/scalameta/scalafmt/issues{/number}","pulls_url":"https://api.github.com/repos/scalameta/scalafmt/pulls{/number}","milestones_url":"https://api.github.com/repos/scalameta/scalafmt/milestones{/number}","notifications_url":"https://api.github.com/repos/scalameta/scalafmt/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/scalameta/scalafmt/labels{/name}","releases_url":"https://api.github.com/repos/scalameta/scalafmt/releases{/id}","deployments_url":"https://api.github.com/repos/scalameta/scalafmt/deployments","created_at":"2015-11-02T21:34:40Z","updated_at":"2017-12-11T02:41:59Z","pushed_at":"2017-12-06T17:42:17Z","git_url":"git://github.com/scalameta/scalafmt.git","ssh_url":"git@github.com:scalameta/scalafmt.git","clone_url":"https://github.com/scalameta/scalafmt.git","svn_url":"https://github.com/scalameta/scalafmt","homepage":"http://scalameta.org/scalafmt","size":4239,"stargazers_count":537,"watchers_count":537,"language":"Scala","has_issues":true,"has_projects":false,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":122,"mirror_url":null,"archived":false,"open_issues_count":163,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0"},"forks":122,"open_issues":163,"watchers":537,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/scalameta/scalafmt/pulls/1085"},"html":{"href":"https://github.com/scalameta/scalafmt/pull/1085"},"issue":{"href":"https://api.github.com/repos/scalameta/scalafmt/issues/1085"},"comments":{"href":"https://api.github.com/repos/scalameta/scalafmt/issues/1085/comments"},"review_comments":{"href":"https://api.github.com/repos/scalameta/scalafmt/pulls/1085/comments"},"review_comment":{"href":"https://api.github.com/repos/scalameta/scalafmt/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/scalameta/scalafmt/pulls/1085/commits"},"statuses":{"href":"https://api.github.com/repos/scalameta/scalafmt/statuses/7b52844607a2c6128c449a22745e80e47ec9bf71"}},"author_association":"COLLABORATOR"}}
{"actor":{"display_login":"vovapolu"}}