@@ -68,6 +68,7 @@ int main(string[] args)
6868 " test" : &testMain,
6969 " init" : &initMain,
7070 " install" : &installMain,
71+ " use" : &useMain,
7172 // "watch": &watchMain,
7273 " run" : cast (int function (string []))null
7374 ];
@@ -274,21 +275,27 @@ int installMain(string[] args)
274275 import std.string ;
275276 import redub.cli.dub;
276277 import redub.logging;
278+ import redub.misc.dmd_install;
277279 setLogLevel(LogLevel.info);
278280 if (args.length == 1 )
279281 {
280282 error(" redub install requires 1 additional argument: " ,
281283 " \n\t opend: installs opend" ,
282- " \n\t ldc <version?|? >: installs ldc latest if version is unspecified." ,
284+ " \n\t ldc <version?|help >: installs ldc latest if version is unspecified." ,
283285 " \n\t\t help: Lists available ldc versions" ,
286+ " \n\t dmd <version?>: installs the dmd with the version " ~ DefaultDMDVersion~ " if version is unspecified"
284287 );
285288 return 1 ;
286289 }
287290 string compiler = args[1 ];
288291 if (compiler.startsWith(" opend" ))
289292 {
290293 import redub.misc.opend_install;
291- return installOpend ();
294+ if (! installOpend())
295+ {
296+ error(" Could not install OpenD" );
297+ return 1 ;
298+ }
292299 }
293300 else if (compiler.startsWith(" ldc" ))
294301 {
@@ -313,11 +320,110 @@ int installMain(string[] args)
313320 }
314321 return 0 ;
315322 }
316- return cast (int )installLdc(ldcVer);
323+ if (! installLdc(ldcVer))
324+ {
325+ error(" Could not install LDC " , ldcVer);;
326+ return 1 ;
327+ }
317328 }
318- else if (compiler.startsWith(" dmd" ))
329+ else if (compiler == " dmd" )
330+ {
331+ import redub.misc.dmd_install;
332+ string dmdVer = args.length > 2 ? args[2 ] : DefaultDMDVersion;
333+ if (! installDmd(dmdVer))
334+ {
335+ error(" Could not install DMD " , dmdVer);;
336+ return 1 ;
337+ }
338+ }
339+ return 0 ;
340+ }
341+
342+ int useMain (string [] args)
343+ {
344+ import std.string ;
345+ import std.file ;
346+ import redub.cli.dub;
347+ import redub.logging;
348+ import redub.meta;
349+ import redub.misc.path;
350+ import redub.misc.dmd_install;
351+ JSONValue meta = getRedubMeta();
352+ setLogLevel(LogLevel.info);
353+ if (args.length == 1 )
354+ {
355+ error(" redub use requires 1 additional argument: " ,
356+ " \n\t opend <dmd|ldc>: uses the wanted opend compiler as the default" ,
357+ " \n\t ldc <version?>: uses the latest ldc latest if version is unspecified." ,
358+ " \n\t dmd <version?>: uses the " ~ DefaultDMDVersion~ " dmd if the version is unspecified." ,
359+ " \n\t reset: removes the default compiler and redub will set it again by the first one found in the PATH environment variable" ,
360+ );
361+ return 1 ;
362+ }
363+ string compiler = args[1 ];
364+ if (compiler.startsWith(" opend" ))
365+ {
366+ import redub.misc.opend_install;
367+ import redub.compiler_identification;
368+ string opendCompiler = args.length > 2 ? args[2 ] : null ;
369+ if (opendCompiler != " dmd" && opendCompiler != " ldc2" )
370+ {
371+ error(" redub uses opend: requires either dmd or ldc2 as an argument" );
372+ return 1 ;
373+ }
374+ string opendFolder = getOpendFolder();
375+ if (! exists(opendFolder))
376+ installOpend();
377+ version (Windows )
378+ opendCompiler~= " .exe" ;
379+ string opendBin = buildNormalizedPath(opendFolder, opendCompiler);
380+ saveGlobalCompiler(opendBin, meta, true , false );
381+ }
382+ else if (compiler == " ldc" || compiler == " ldc2" )
383+ {
384+ import redub.api;
385+ import redub.misc.ldc_install;
386+ import redub.misc.github_tag_check;
387+ enum ldcRepo = " ldc-developers/ldc" ;
388+ string ldcVer = args.length > 2 ? args[2 ] : null ;
389+ if (! ldcVer)
390+ ldcVer = getLatestGitRepositoryTag(ldcRepo);
391+ string ldcFolder = getLdcFolder(ldcVer);
392+ if (! exists(ldcFolder))
393+ installLdc(ldcVer);
394+ string ldcBin = " ldc2" ;
395+ version (Windows )
396+ ldcBin~= " .exe" ;
397+ ldcBin = buildNormalizedPath(ldcFolder, ldcBin);
398+ saveGlobalCompiler(ldcBin, meta, true , false );
399+ }
400+ else if (compiler == " dmd" )
401+ {
402+ import redub.misc.dmd_install;
403+ string dmdVer = args.length > 2 ? args[2 ] : DefaultDMDVersion;
404+ string dmdFolder = getDmdFolder(dmdVer);
405+ if (! exists(dmdFolder) && ! installDmd(dmdVer))
406+ {
407+ error(" Could not install DMD for using it." );
408+ return 1 ;
409+ }
410+ string dmdBin = " dmd" ;
411+ version (Windows )
412+ dmdBin~= " .exe" ;
413+ dmdBin = buildNormalizedPath(dmdFolder, dmdBin);
414+ saveGlobalCompiler(dmdBin, meta, true , false );
415+ }
416+ else if (compiler.startsWith(" reset" ))
417+ {
418+ meta.data.object.remove(" defaultCompiler" );
419+ meta.data.object.remove(" globalPaths" );
420+ infos(" Default redub compiler is now reset." );
421+ }
422+
423+ if (" defaultCompiler" in meta)
319424 {
320- // return installDmd( );
425+ infos(meta[ " globalPaths " ][meta[ " defaultCompiler " ].str].str, " is now the default compiler " );
321426 }
427+ saveRedubMeta(meta);
322428 return 0 ;
323429}
0 commit comments