| 1 |
/* |
| 2 |
* Copyright (C)2005-2012 Haxe Foundation |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 |
* DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
package legacyhaxelib; |
| 23 |
import haxe.zip.Reader; |
| 24 |
import haxe.zip.Entry; |
| 25 |
|
| 26 |
import haxe.xml.Check; |
| 27 |
|
| 28 |
typedef UserInfos = { |
| 29 |
var name : String; |
| 30 |
var fullname : String; |
| 31 |
var email : String; |
| 32 |
var projects : Array<String>; |
| 33 |
} |
| 34 |
|
| 35 |
typedef VersionInfos = { |
| 36 |
var date : String; |
| 37 |
var name : String; |
| 38 |
var comments : String; |
| 39 |
} |
| 40 |
|
| 41 |
typedef ProjectInfos = { |
| 42 |
var name : String; |
| 43 |
var desc : String; |
| 44 |
var website : String; |
| 45 |
var owner : String; |
| 46 |
var license : String; |
| 47 |
var curversion : String; |
| 48 |
var versions : Array<VersionInfos>; |
| 49 |
var tags : List<String>; |
| 50 |
} |
| 51 |
|
| 52 |
typedef XmlInfos = { |
| 53 |
var project : String; |
| 54 |
var website : String; |
| 55 |
var desc : String; |
| 56 |
var license : String; |
| 57 |
var version : String; |
| 58 |
var versionComments : String; |
| 59 |
var developers : List<String>; |
| 60 |
var tags : List<String>; |
| 61 |
var dependencies : List<{ project : String, version : String }>; |
| 62 |
} |
| 63 |
|
| 64 |
class Data { |
| 65 |
|
| 66 |
public static var XML = "haxelib.xml"; |
| 67 |
public static var DOCXML = "haxedoc.xml"; |
| 68 |
public static var REPOSITORY = "files"; |
| 69 |
public static var alphanum = ~/^[A-Za-z0-9_.-]+$/; |
| 70 |
static var LICENSES = ["GPL","LGPL","BSD","Public","MIT"]; |
| 71 |
|
| 72 |
static function requiredAttribute( x : Xml, name ) { |
| 73 |
var v = x.get(name); |
| 74 |
if( v == null ) |
| 75 |
throw "Missing required attribute '"+name+"' in node "+x.nodeName; |
| 76 |
return v; |
| 77 |
} |
| 78 |
|
| 79 |
static function requiredNode( x : Xml, name ) { |
| 80 |
var v = x.elementsNamed(name).next(); |
| 81 |
if( v == null ) |
| 82 |
throw "Missing required node '"+name+"' in node "+x.nodeName; |
| 83 |
return v; |
| 84 |
} |
| 85 |
|
| 86 |
static function requiredText( x : Xml ) { |
| 87 |
var v = x.firstChild(); |
| 88 |
if( v == null || (v.nodeType != Xml.PCData && v.nodeType != Xml.CData) ) |
| 89 |
throw "Missing required text in node "+x.nodeName; |
| 90 |
return v.nodeValue; |
| 91 |
} |
| 92 |
|
| 93 |
public static function safe( name : String ) { |
| 94 |
if( !alphanum.match(name) ) |
| 95 |
throw "Invalid parameter : "+name; |
| 96 |
return name.split(".").join(","); |
| 97 |
} |
| 98 |
|
| 99 |
public static function unsafe( name : String ) { |
| 100 |
return name.split(",").join("."); |
| 101 |
} |
| 102 |
|
| 103 |
public static function fileName( lib : String, ver : String ) { |
| 104 |
return safe(lib)+"-"+safe(ver)+".zip"; |
| 105 |
} |
| 106 |
|
| 107 |
public static function readDoc( zip : List<Entry> ) : String { |
| 108 |
for( f in zip ) |
| 109 |
if( StringTools.endsWith(f.fileName,DOCXML) ) |
| 110 |
return Reader.unzip(f).toString(); |
| 111 |
return null; |
| 112 |
} |
| 113 |
|
| 114 |
public static function readInfos( zip : List<Entry>, check : Bool ) : XmlInfos { |
| 115 |
var xmldata = null; |
| 116 |
for( f in zip ) |
| 117 |
if( StringTools.endsWith(f.fileName,XML) ) { |
| 118 |
xmldata = Reader.unzip(f).toString(); |
| 119 |
break; |
| 120 |
} |
| 121 |
if( xmldata == null ) |
| 122 |
throw XML+" not found in package"; |
| 123 |
return readData(xmldata,check); |
| 124 |
} |
| 125 |
|
| 126 |
static function doCheck( doc : Xml ) { |
| 127 |
var sname = Att("name",FReg(alphanum)); |
| 128 |
var schema = RNode( |
| 129 |
"project", |
| 130 |
[ sname, Att("url"), Att("license",FEnum(LICENSES)) ], |
| 131 |
RList([ |
| 132 |
RMulti( RNode("user",[sname]), true ), |
| 133 |
RMulti( RNode("tag",[Att("v",FReg(alphanum))]) ), |
| 134 |
RNode("description",[],RData()), |
| 135 |
RNode("version",[sname],RData()), |
| 136 |
RMulti( RNode("depends",[sname,Att("version",FReg(alphanum),"")]) ), |
| 137 |
]) |
| 138 |
); |
| 139 |
haxe.xml.Check.checkDocument(doc,schema); |
| 140 |
} |
| 141 |
|
| 142 |
public static function readData( xmldata : String, check : Bool ) : XmlInfos { |
| 143 |
var doc = Xml.parse(xmldata); |
| 144 |
if( check ) |
| 145 |
doCheck(doc); |
| 146 |
var p = new haxe.xml.Fast(doc).node.project; |
| 147 |
var project = p.att.name; |
| 148 |
if( project.length < 3 ) |
| 149 |
throw "Project name must contain at least 3 characters"; |
| 150 |
var tags = new List(); |
| 151 |
for( t in p.nodes.tag ) |
| 152 |
tags.add(t.att.v.toLowerCase()); |
| 153 |
var devs = new List(); |
| 154 |
for( d in p.nodes.user ) |
| 155 |
devs.add(d.att.name); |
| 156 |
var deps = new List(); |
| 157 |
for( d in p.nodes.depends ) |
| 158 |
deps.add({ project : d.att.name, version : if( d.has.version ) d.att.version else "" }); |
| 159 |
return { |
| 160 |
project : project, |
| 161 |
website : p.att.url, |
| 162 |
desc : p.node.description.innerData, |
| 163 |
version : p.node.version.att.name, |
| 164 |
versionComments : p.node.version.innerData, |
| 165 |
license : p.att.license, |
| 166 |
tags : tags, |
| 167 |
developers : devs, |
| 168 |
dependencies : deps, |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
} |