this is a fetch program designed for use with opend, to pull in repos that were setup for dub. usage: `fetch *repo*`
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.2KB

  1. import std.stdio;
  2. import std.file;
  3. import std.conv;
  4. import std.process;
  5. import std.array;
  6. import std.algorithm;
  7. bool run_script(string script_command){
  8. try{
  9. writeln("calling::: " ~ script_command);
  10. auto cont = executeShell(script_command);
  11. if(cont.status != 0) {
  12. writeln("failed to run script command" ~cont.output);
  13. //return "Failed";
  14. return false;
  15. } else {
  16. writeln("script output " ~ cont.output);
  17. return true;
  18. }
  19. } catch (Exception e) {
  20. writeln("Failed to execute script");
  21. return false;
  22. }
  23. }
  24. int main(string[] args) {
  25. writeln(args.length);
  26. if(args.length <= 1){
  27. writeln("You must provide a repo: fetch git.dreamphase.net/username/repo.git");
  28. return 1;
  29. }
  30. auto repo = args[1];
  31. if(repo == "clean"){
  32. try{
  33. if(getcwd() ~"/external/".exists){
  34. writeln("Removing all repos::\r\n");
  35. auto files = dirEntries(getcwd() ~"/external/", SpanMode.shallow).array();
  36. writeln(files);
  37. foreach (fpath; files) {
  38. auto delete_split = to!string(fpath).split("/");
  39. auto delete_path = getcwd() ~ "/" ~ delete_split[$-1];
  40. string clean_script = "rm " ~ delete_path;
  41. run_script(clean_script);
  42. clean_script = "rm " ~ delete_path ~ ".d";
  43. run_script(clean_script);
  44. }
  45. string clean_script = "rm -rf external/";
  46. run_script(clean_script);
  47. return 0;
  48. } else {
  49. writeln("Nothing to clean. `external` path doesn't exist.");
  50. return 1;
  51. }
  52. } catch (Exception e){
  53. writeln("Unable to clean");
  54. writeln(e.msg); // "error"
  55. writeln(e.file); // __FILE__
  56. writeln(e.line); // __LINE__ - 7
  57. }
  58. }
  59. try{
  60. writeln("Trying to fetcht: \r\n" ~ repo);
  61. string clone_script = "mkdir -p external >> /dev/null && cd external && git clone " ~ repo;
  62. auto result = run_script(clone_script);
  63. writeln("Clone output:: \r\n" ~result);
  64. writeln("repo has been cloned, setting up link.");
  65. auto repo_split = repo.split("/");
  66. auto package_name = repo_split[repo_split.length-1].replace(".git","");
  67. writeln("Package name: " ~ package_name);
  68. string package_base = getcwd() ~ "/external/" ~ package_name ~ "/source/";
  69. if(package_base.exists){
  70. auto files = dirEntries(package_base, SpanMode.shallow).array();
  71. writeln("PACKAGE path: \r\n");
  72. writeln(files);
  73. if(files.length >1){
  74. writeln("Unable to locate package internals.");
  75. return 1;
  76. }
  77. auto package_path = to!string(files[0]);
  78. if(!package_path.canFind(".d")){
  79. //package_path = package_path ~ "/package.d";
  80. auto auto_package_name = package_path.split("/")[$-1];
  81. string link_script = "ln -s " ~ package_path.replace(".d","") ~ " " ~ auto_package_name;
  82. auto link_result = run_script(link_script);
  83. } else {
  84. string link_script = "ln -s " ~ package_path ~ " " ~ package_name ~ ".d";
  85. auto link_result = run_script(link_script);
  86. }
  87. } else {
  88. writeln("Unable to locate package internals.");
  89. return 1;
  90. }
  91. } catch (Exception e){
  92. writeln(e.msg); // "error"
  93. writeln(e.file); // __FILE__
  94. writeln(e.line); // __LINE__ - 7
  95. }
  96. return 0;
  97. }