Data.hx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 tools.haxelib;
  23. import haxe.zip.Reader;
  24. import haxe.zip.Entry;
  25. import haxe.Json;
  26. using StringTools;
  27. typedef UserInfos = {
  28. var name : String;
  29. var fullname : String;
  30. var email : String;
  31. var projects : Array<String>;
  32. }
  33. typedef VersionInfos = {
  34. var date : String;
  35. var name : String;
  36. var comments : String;
  37. }
  38. typedef ProjectInfos = {
  39. var name : String;
  40. var desc : String;
  41. var website : String;
  42. var owner : String;
  43. var license : String;
  44. var curversion : String;
  45. var versions : Array<VersionInfos>;
  46. var tags : List<String>;
  47. }
  48. typedef Infos = {
  49. var project : String;
  50. var website : String;
  51. var desc : String;
  52. var license : String;
  53. var version : String;
  54. var versionComments : String;
  55. var developers : List<String>;
  56. var tags : List<String>;
  57. var dependencies : List<{ project : String, version : String }>;
  58. }
  59. class Data {
  60. public static var JSON = "haxelib.json";
  61. public static var DOCXML = "haxedoc.xml";
  62. public static var REPOSITORY = "files/3.0";
  63. public static var alphanum = ~/^[A-Za-z0-9_.-]+$/;
  64. static var LICENSES = ["GPL","LGPL","BSD","Public","MIT"];
  65. static var RESERVED_NAMES = ["haxe","all"];
  66. public static function safe( name : String ) {
  67. if( !alphanum.match(name) )
  68. throw "Invalid parameter : "+name;
  69. return name.split(".").join(",");
  70. }
  71. public static function unsafe( name : String ) {
  72. return name.split(",").join(".");
  73. }
  74. public static function fileName( lib : String, ver : String ) {
  75. return safe(lib)+"-"+safe(ver)+".zip";
  76. }
  77. public static function locateBasePath( zip : List<Entry> ) {
  78. for( f in zip ) {
  79. if( StringTools.endsWith(f.fileName,JSON) ) {
  80. return f.fileName.substr(0,f.fileName.length - JSON.length);
  81. }
  82. }
  83. throw "No "+JSON+" found";
  84. }
  85. public static function readDoc( zip : List<Entry> ) : String {
  86. for( f in zip )
  87. if( StringTools.endsWith(f.fileName,DOCXML) )
  88. return Reader.unzip(f).toString();
  89. return null;
  90. }
  91. public static function readInfos( zip : List<Entry>, check : Bool ) : Infos {
  92. var infodata = null;
  93. for( f in zip )
  94. if( StringTools.endsWith(f.fileName,JSON) ) {
  95. infodata = Reader.unzip(f).toString();
  96. break;
  97. }
  98. if( infodata == null )
  99. throw JSON + " not found in package";
  100. return readData(infodata,check);
  101. }
  102. static function doCheck( doc : Dynamic ) {
  103. var libName = doc.name.toLowerCase();
  104. if ( Lambda.indexOf(RESERVED_NAMES, libName) > -1 )
  105. throw 'Library name "${doc.name}" is reserved. Please choose another name';
  106. if ( libName.endsWith(".zip") )
  107. throw 'Library name cannot end in ".zip". Please choose another name';
  108. if ( libName.endsWith(".hxml") )
  109. throw 'Library name cannot end in ".hxml". Please choose another name';
  110. if( Lambda.indexOf(LICENSES, doc.license) == -1 )
  111. throw "License must be one of the following: " + LICENSES;
  112. switch Type.typeof(doc.contributors) {
  113. case TNull: throw "At least one contributor must be included";
  114. //case TClass(String): doc.contributors = [doc.contributors];
  115. case TClass(Array):
  116. default: throw 'invalid type for contributors';
  117. }
  118. switch Type.typeof(doc.version) {
  119. case TClass(String):
  120. SemVer.ofString(doc.version);
  121. default: throw 'version must be defined as string';
  122. }
  123. switch Type.typeof(doc.tags) {
  124. case TClass(Array), TNull:
  125. default: throw 'tags must be defined as array';
  126. }
  127. switch Type.typeof(doc.dependencies) {
  128. case TObject, TNull:
  129. default: throw 'dependencies must be defined as object';
  130. }
  131. switch Type.typeof(doc.releasenote) {
  132. case TClass(String):
  133. case TNull: throw 'no releasenote specified';
  134. default: throw 'releasenote should be string';
  135. }
  136. }
  137. public static function readData( jsondata: String, check : Bool ) : Infos {
  138. var doc = try Json.parse(jsondata) catch( e : Dynamic ) throw "Error in JSON data : " + e;
  139. if( check )
  140. doCheck(doc);
  141. var project:String = doc.name;
  142. if( project.length < 3 )
  143. throw "Project name must contain at least 3 characters";
  144. var tags = new List();
  145. if( doc.tags != null) {
  146. var tagsArray:Array<String> = doc.tags;
  147. for( t in tagsArray )
  148. tags.add(t);
  149. }
  150. var devs = new List();
  151. var developers:Array<String> = doc.contributors;
  152. for( d in developers )
  153. devs.add(d);
  154. var deps = new List();
  155. if( doc.dependencies != null ) {
  156. for( d in Reflect.fields(doc.dependencies) ) {
  157. deps.add({ project: d, version: Std.string(Reflect.field(doc.dependencies, d)) });
  158. }
  159. }
  160. return {
  161. project : project,
  162. website : doc.url,
  163. desc : doc.description,
  164. version : doc.version,
  165. versionComments : doc.releasenote,
  166. license : doc.license,
  167. tags : tags,
  168. developers : devs,
  169. dependencies : deps
  170. };
  171. }
  172. }