1
1
package scala .cli .commands .export0
2
2
3
3
import caseapp .*
4
+ import com .github .plokhotnyuk .jsoniter_scala .core .*
5
+ import com .github .plokhotnyuk .jsoniter_scala .macros .JsonCodecMaker
6
+ import com .google .gson .{Gson , GsonBuilder }
4
7
import coursier .cache .FileCache
5
8
import coursier .util .{Artifact , Task }
6
9
10
+ import java .io .{OutputStreamWriter , PrintStream }
11
+ import java .nio .charset .{Charset , StandardCharsets }
12
+
7
13
import scala .build .EitherCps .{either , value }
8
14
import scala .build .*
9
15
import scala .build .errors .BuildException
10
16
import scala .build .input .Inputs
11
17
import scala .build .internal .{Constants , CustomCodeWrapper }
12
- import scala .build .options .{BuildOptions , Scope }
18
+ import scala .build .options .{BuildOptions , Platform , Scope }
13
19
import scala .cli .CurrentParams
14
20
import scala .cli .commands .ScalaCommand
15
21
import scala .cli .commands .shared .SharedOptions
16
22
import scala .cli .exportCmd .*
23
+ import scala .util .Using
17
24
18
25
object Export extends ScalaCommand [ExportOptions ] {
19
26
override def scalaSpecificationLevel = SpecificationLevel .RESTRICTED
@@ -53,9 +60,17 @@ object Export extends ScalaCommand[ExportOptions] {
53
60
}
54
61
55
62
// FIXME Auto-update those
56
- def sbtBuildTool (extraSettings : Seq [String ], sbtVersion : String , logger : Logger ): Sbt =
57
- Sbt (sbtVersion, extraSettings, logger)
58
- def millBuildTool (cache : FileCache [Task ], projectName : Option [String ], logger : Logger ): Mill = {
63
+ def sbtProjectDescriptor (
64
+ extraSettings : Seq [String ],
65
+ sbtVersion : String ,
66
+ logger : Logger
67
+ ): SbtProjectDescriptor =
68
+ SbtProjectDescriptor (sbtVersion, extraSettings, logger)
69
+ def millProjectDescriptor (
70
+ cache : FileCache [Task ],
71
+ projectName : Option [String ],
72
+ logger : Logger
73
+ ): MillProjectDescriptor = {
59
74
val launcherArtifacts = Seq (
60
75
os.rel / " mill" -> s " https://github.com/lefou/millw/raw/ ${Constants .lefouMillwRef}/millw " ,
61
76
os.rel / " mill.bat" -> s " https://github.com/lefou/millw/raw/ ${Constants .lefouMillwRef}/millw.bat "
@@ -73,9 +88,12 @@ object Export extends ScalaCommand[ExportOptions] {
73
88
}
74
89
val launchersTask = cache.logger.using(Task .gather.gather(launcherTasks))
75
90
val launchers = launchersTask.unsafeRun()(cache.ec)
76
- Mill (Constants .millVersion, projectName, launchers, logger)
91
+ MillProjectDescriptor (Constants .millVersion, projectName, launchers, logger)
77
92
}
78
93
94
+ def jsonProjectDescriptor (projectName : Option [String ], logger : Logger ): JsonProjectDescriptor =
95
+ JsonProjectDescriptor (projectName, logger)
96
+
79
97
override def sharedOptions (opts : ExportOptions ): Option [SharedOptions ] = Some (opts.shared)
80
98
81
99
override def runCommand (options : ExportOptions , args : RemainingArgs , logger : Logger ): Unit = {
@@ -84,24 +102,29 @@ object Export extends ScalaCommand[ExportOptions] {
84
102
val output = options.output.getOrElse(" dest" )
85
103
val dest = os.Path (output, os.pwd)
86
104
if (os.exists(dest)) {
87
- System .err.println (
105
+ logger.error (
88
106
s """ Error: $dest already exists.
89
107
|To change the destination output directory pass --output path or remove the destination directory first. """ .stripMargin
90
108
)
91
109
sys.exit(1 )
92
110
}
93
111
112
+ val shouldExportToJson = options.json.getOrElse(false )
94
113
val shouldExportToMill = options.mill.getOrElse(false )
95
114
val shouldExportToSbt = options.sbt.getOrElse(false )
96
115
if (shouldExportToMill && shouldExportToSbt) {
97
- System .err.println (
116
+ logger.error (
98
117
s " Error: Cannot export to both mill and sbt. Please pick one build tool to export. "
99
118
)
100
119
sys.exit(1 )
101
120
}
102
121
103
- val buildToolName = if (shouldExportToMill) " mill" else " sbt"
104
- System .out.println(s " Exporting to a $buildToolName project... " )
122
+ if (! shouldExportToJson) {
123
+ val buildToolName = if (shouldExportToMill) " mill" else " sbt"
124
+ logger.message(s " Exporting to a $buildToolName project... " )
125
+ }
126
+ else
127
+ logger.message(s " Exporting to JSON... " )
105
128
106
129
val inputs = options.shared.inputs(args.all).orExit(logger)
107
130
CurrentParams .workspaceOpt = Some (inputs.workspace)
@@ -131,7 +154,7 @@ object Export extends ScalaCommand[ExportOptions] {
131
154
svMain <- optionsMain0.scalaOptions.scalaVersion
132
155
svTest <- optionsTest0.scalaOptions.scalaVersion
133
156
} if (svMain != svTest) {
134
- System .err.println (
157
+ logger.error (
135
158
s """ Detected different Scala versions in main and test scopes.
136
159
|Please set the Scala version explicitly in the main and test scope with using directives or pass -S, --scala-version as parameter """ .stripMargin
137
160
)
@@ -141,27 +164,30 @@ object Export extends ScalaCommand[ExportOptions] {
141
164
if (
142
165
optionsMain0.scalaOptions.scalaVersion.isEmpty && optionsTest0.scalaOptions.scalaVersion.nonEmpty
143
166
) {
144
- System .err.println (
167
+ logger.error (
145
168
s """ Detected that the Scala version is only set in test scope.
146
169
|Please set the Scala version explicitly in the main and test scopes with using directives or pass -S, --scala-version as parameter """ .stripMargin
147
170
)
148
171
sys.exit(1 )
149
172
}
150
173
151
174
val sbtVersion = options.sbtVersion.getOrElse(" 1.6.1" )
152
- def sbtBuildTool0 =
153
- sbtBuildTool(options.sbtSetting.map(_.trim).filter(_.nonEmpty), sbtVersion, logger)
154
175
155
- val buildTool =
176
+ def sbtProjectDescriptor0 =
177
+ sbtProjectDescriptor(options.sbtSetting.map(_.trim).filter(_.nonEmpty), sbtVersion, logger)
178
+
179
+ val projectDescriptor =
156
180
if (shouldExportToMill)
157
- millBuildTool(options.shared.coursierCache, options.project, logger)
181
+ millProjectDescriptor(options.shared.coursierCache, options.project, logger)
182
+ else if (shouldExportToJson)
183
+ jsonProjectDescriptor(options.project, logger)
158
184
else // shouldExportToSbt isn't checked, as it's treated as default
159
- sbtBuildTool0
185
+ sbtProjectDescriptor0
160
186
161
- val project = buildTool .`export`(optionsMain0, optionsTest0, sourcesMain, sourcesTest)
187
+ val project = projectDescriptor .`export`(optionsMain0, optionsTest0, sourcesMain, sourcesTest)
162
188
163
189
os.makeDir.all(dest)
164
190
project.writeTo(dest)
165
- System .out.println (s " Exported to: $dest" )
191
+ logger.message (s " Exported to: $dest" )
166
192
}
167
193
}
0 commit comments