1 module beangle.micdn.maven.config;
2 import std.string;
3 import dxml.dom;
4 import std.conv;
5 import beangle.xml.reader;
6 
7 class Config{
8   /**artifact local repo*/
9   immutable string base;
10   /**cache the artifacts*/
11   immutable bool cacheable;
12   /**enable dir list*/
13   immutable bool publicList;
14   /**candinates remote repos*/
15   immutable string[] remoteRepos=[];
16   /**default remote repo*/
17   immutable string defaultRepo;
18 
19   static auto CentralURL = "https://repo1.maven.org/maven2";
20   static auto AliyunURL = "https://maven.aliyun.com/nexus/content/groups/public";
21 
22   this(string base,bool cacheable,bool publicList,string[] remoteRepos){
23     this.base=base;
24     this.cacheable=cacheable;
25     this.publicList=publicList;
26     this.remoteRepos=to!(immutable(string[]))(remoteRepos);
27     this.defaultRepo=remoteRepos[$-1];
28   }
29 
30   public static Config parse(string content){
31     auto dom = parseDOM!simpleXML( content).children[0];
32     auto attrs = getAttrs( dom);
33     bool cacheable = attrs.get( "cacheable","true").to!bool;
34     bool publicList = attrs.get( "publicList","false").to!bool;
35     import std.path;
36     string base = expandTilde( attrs.get( "base","~/.m2/repository"));
37     string[] remoteRepos=[];
38     auto remotesEntries = children( dom,"remotes");
39     if (!remotesEntries.empty){
40       auto remoteEntries = children( remotesEntries.front,"remote");
41       foreach (remoteEntry;remoteEntries){
42         attrs = getAttrs( remoteEntry);
43         if ("url" in attrs){
44           remoteRepos.add( attrs["url"]);
45         }else if ("alias" in attrs){
46           switch ( attrs["alias"]){
47             case "central": remoteRepos.add( CentralURL); break ;
48             case "aliyun":remoteRepos.add( AliyunURL);break ;
49             default: throw new Exception( "unknown named repo "~ attrs["alias"] );
50           }
51         }
52       }
53     }
54     if (remoteRepos.length==0){
55       remoteRepos.add( CentralURL);
56     }
57     return new Config( base,cacheable,publicList,remoteRepos);
58   }
59 
60 }
61 
62 void add(ref string[] remotes,string remote){
63   remotes.length+=1;
64   remotes[$-1]=remote;
65 }
66 unittest{
67   auto content=`<?xml version="1.0" encoding="UTF-8"?>
68 <maven cacheable="true" >
69   <remotes>
70     <remote url="https://maven.aliyun.com/nexus/content/groups/public"/>
71     <remote alias="central"/>
72   </remotes>
73 </maven>`;
74 
75   auto config = Config.parse( content);
76   assert( config.remoteRepos.length==2);
77   assert( config.remoteRepos[1] == Config.CentralURL);
78 }
79