PageRenderTime 28ms CodeModel.GetById 15ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/bin/std/tools/haxelib/Datas.hx

http://github.com/Yoomee/clippy
Haxe | 150 lines | 132 code | 18 blank | 0 comment | 24 complexity | 951ad79e5bb1b445c37c63157acb926f MD5 | raw file
  1package tools.haxelib;
  2
  3import neko.zip.Reader;
  4import haxe.xml.Check;
  5
  6typedef UserInfos = {
  7	var name : String;
  8	var fullname : String;
  9	var email : String;
 10	var projects : Array<String>;
 11}
 12
 13typedef VersionInfos = {
 14	var date : String;
 15	var name : String;
 16	var comments : String;
 17}
 18
 19typedef ProjectInfos = {
 20	var name : String;
 21	var desc : String;
 22	var website : String;
 23	var owner : String;
 24	var license : String;
 25	var curversion : String;
 26	var versions : Array<VersionInfos>;
 27	var tags : List<String>;
 28}
 29
 30typedef XmlInfos = {
 31	var project : String;
 32	var website : String;
 33	var desc : String;
 34	var license : String;
 35	var version : String;
 36	var versionComments : String;
 37	var developers : List<String>;
 38	var tags : List<String>;
 39	var dependencies : List<{ project : String, version : String }>;
 40}
 41
 42class Datas {
 43
 44	public static var XML = "haxelib.xml";
 45	public static var DOCXML = "haxedoc.xml";
 46	public static var REPOSITORY = "files";
 47	public static var alphanum = ~/^[A-Za-z0-9_.-]+$/;
 48	static var LICENSES = ["GPL","LGPL","BSD","Public"];
 49
 50	static function requiredAttribute( x : Xml, name ) {
 51		var v = x.get(name);
 52		if( v == null )
 53			throw "Missing required attribute '"+name+"' in node "+x.nodeName;
 54		return v;
 55	}
 56
 57	static function requiredNode( x : Xml, name ) {
 58		var v = x.elementsNamed(name).next();
 59		if( v == null )
 60			throw "Missing required node '"+name+"' in node "+x.nodeName;
 61		return v;
 62	}
 63
 64	static function requiredText( x : Xml ) {
 65		var v = x.firstChild();
 66		if( v == null || (v.nodeType != Xml.PCData && v.nodeType != Xml.CData) )
 67			throw "Missing required text in node "+x.nodeName;
 68		return v.nodeValue;
 69	}
 70
 71	public static function safe( name : String ) {
 72		if( !alphanum.match(name) )
 73			throw "Invalid parameter : "+name;
 74		return name.split(".").join(",");
 75	}
 76
 77	public static function unsafe( name : String ) {
 78		return name.split(",").join(".");
 79	}
 80
 81	public static function fileName( lib : String, ver : String ) {
 82		return safe(lib)+"-"+safe(ver)+".zip";
 83	}
 84
 85	public static function readDoc( zip : List<ZipEntry> ) : String {
 86		for( f in zip )
 87			if( StringTools.endsWith(f.fileName,DOCXML) )
 88				return neko.zip.Reader.unzip(f).toString();
 89		return null;
 90	}
 91
 92	public static function readInfos( zip : List<ZipEntry>, check : Bool ) : XmlInfos {
 93		var xmldata = null;
 94		for( f in zip )
 95			if( StringTools.endsWith(f.fileName,XML) ) {
 96				xmldata = neko.zip.Reader.unzip(f).toString();
 97				break;
 98			}
 99		if( xmldata == null )
100			throw XML+" not found in package";
101		return readData(xmldata,check);
102	}
103
104	static function doCheck( doc : Xml ) {
105		var sname = Att("name",FReg(alphanum));
106		var schema = RNode(
107			"project",
108			[ sname, Att("url"), Att("license",FEnum(LICENSES)) ],
109			RList([
110				RMulti( RNode("user",[sname]), true ),
111				RMulti( RNode("tag",[Att("v",FReg(alphanum))]) ),
112				RNode("description",[],RData()),
113				RNode("version",[sname],RData()),
114				RMulti(	RNode("depends",[sname,Att("version",FReg(alphanum),"")]) ),
115			])
116		);
117		haxe.xml.Check.checkDocument(doc,schema);
118	}
119
120	public static function readData( xmldata : String, check : Bool ) : XmlInfos {
121		var doc = Xml.parse(xmldata);
122		if( check )
123			doCheck(doc);
124		var p = new haxe.xml.Fast(doc).node.project;
125		var project = p.att.name;
126		if( project.length < 3 )
127			throw "Project name must contain at least 3 characters";
128		var tags = new List();
129		for( t in p.nodes.tag )
130			tags.add(t.att.v.toLowerCase());
131		var devs = new List();
132		for( d in p.nodes.user )
133			devs.add(d.att.name);
134		var deps = new List();
135		for( d in p.nodes.depends )
136			deps.add({ project : d.att.name, version : if( d.has.version ) d.att.version else "" });
137		return {
138			project : project,
139			website : p.att.url,
140			desc : p.node.description.innerData,
141			version : p.node.version.att.name,
142			versionComments : p.node.version.innerData,
143			license : p.att.license,
144			tags : tags,
145			developers : devs,
146			dependencies : deps,
147		}
148	}
149
150}