makepack-dolibarrmodule.pl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. #!/usr/bin/perl
  2. #----------------------------------------------------------------------------
  3. # \file build/makepack-dolibarrmodule.pl
  4. # \brief Package builder (tgz, zip, rpm, deb, exe)
  5. # \author (c)2005-2014 Laurent Destailleur <eldy@users.sourceforge.net>
  6. # \contributor (c)2017 Nicolas ZABOURI <info@inovea-conseil.com>
  7. #----------------------------------------------------------------------------
  8. use Cwd;
  9. $OWNER="ldestailleur";
  10. $GROUP="ldestailleur";
  11. @LISTETARGET=("ZIP"); # Possible packages
  12. %REQUIREMENTTARGET=( # Tool requirement for each package
  13. "TGZ"=>"tar",
  14. "ZIP"=>"7z"
  15. );
  16. %ALTERNATEPATH=(
  17. );
  18. use vars qw/ $REVISION $VERSION /;
  19. $REVISION='1.0';
  20. $VERSION="3.5 (build $REVISION)";
  21. #------------------------------------------------------------------------------
  22. # MAIN
  23. #------------------------------------------------------------------------------
  24. ($DIR=$0) =~ s/([^\/\\]+)$//; ($PROG=$1) =~ s/\.([^\.]*)$//; $Extension=$1;
  25. $DIR||='.'; $DIR =~ s/([^\/\\])[\\\/]+$/$1/;
  26. # Detect OS type
  27. # --------------
  28. if ("$^O" =~ /linux/i || (-d "/etc" && -d "/var" && "$^O" !~ /cygwin/i)) { $OS='linux'; $CR=''; }
  29. elsif (-d "/etc" && -d "/Users") { $OS='macosx'; $CR=''; }
  30. elsif ("$^O" =~ /cygwin/i || "$^O" =~ /win32/i) { $OS='windows'; $CR="\r"; }
  31. if (! $OS) {
  32. print "$PROG.$Extension was not able to detect your OS.\n";
  33. print "Can't continue.\n";
  34. print "$PROG.$Extension aborted.\n";
  35. sleep 2;
  36. exit 1;
  37. }
  38. # Define buildroot
  39. # ----------------
  40. if ($OS =~ /linux/) {
  41. $TEMP=$ENV{"TEMP"}||$ENV{"TMP"}||"/tmp";
  42. }
  43. if ($OS =~ /macos/) {
  44. $TEMP=$ENV{"TEMP"}||$ENV{"TMP"}||"/tmp";
  45. }
  46. if ($OS =~ /windows/) {
  47. $TEMP=$ENV{"TEMP"}||$ENV{"TMP"}||"c:/temp";
  48. $PROGPATH=$ENV{"ProgramFiles"};
  49. }
  50. if (! $TEMP || ! -d $TEMP) {
  51. print "Error: A temporary directory can not be find.\n";
  52. print "Check that TEMP or TMP environment variable is set correctly.\n";
  53. print "$PROG.$Extension aborted.\n";
  54. sleep 2;
  55. exit 2;
  56. }
  57. $BUILDROOT="$TEMP/dolibarr-buildroot";
  58. my $copyalreadydone=0;
  59. my $batch=0;
  60. for (0..@ARGV-1) {
  61. if ($ARGV[$_] =~ /^-*target=(\w+)/i) { $target=$1; $batch=1; }
  62. if ($ARGV[$_] =~ /^-*desti=(.+)/i) { $DESTI=$1; }
  63. if ($ARGV[$_] =~ /^-*prefix=(.+)/i) {
  64. $PREFIX=$1;
  65. $FILENAMESNAPSHOT.="-".$PREFIX;
  66. }
  67. }
  68. $SOURCE="$DIR/..";
  69. $DESTI="$SOURCE/build";
  70. if ($ENV{"DESTIMODULES"}) { $DESTI = $ENV{"DESTIMODULES"}; } # Force output dir if env DESTIMODULES is defined
  71. $NEWDESTI=$DESTI;
  72. print "Makepack for modules version $VERSION\n";
  73. print "Source directory: $SOURCE\n";
  74. print "Target directory: $NEWDESTI\n";
  75. # Ask module
  76. print "Enter name for your module (mymodule, mywonderfulmondule, ... or 'all') : ";
  77. $PROJECTINPUT=<STDIN>;
  78. chomp($PROJECTINPUT);
  79. print "Move to ".$DIR." directory.\n";
  80. chdir($DIR);
  81. my @PROJECTLIST=();
  82. if ($PROJECTINPUT eq "all")
  83. {
  84. opendir(DIR, $DIR) || return;
  85. local @rv = grep { /^makepack\-(.*)\.conf$/ } sort readdir(DIR);
  86. closedir(DIR);
  87. foreach my $xxx (0..@rv-1) {
  88. if ($rv[$xxx] =~ /^makepack\-(.*)\.conf$/)
  89. {
  90. @PROJECTLIST[$xxx]=$1;
  91. }
  92. }
  93. }
  94. else
  95. {
  96. @PROJECTLIST=($PROJECTINPUT);
  97. }
  98. # Loop on each projects
  99. foreach my $PROJECT (@PROJECTLIST) {
  100. $PROJECTLC=lc($PROJECT);
  101. if (! -f "makepack-".$PROJECT.".conf")
  102. {
  103. print "Error: can't open conf file makepack-".$PROJECT.".conf\n";
  104. print "\n";
  105. print "For help on building a module package, see web page\n";
  106. print "http://wiki.dolibarr.org/index.php/Module_development#Create_a_package_to_distribute_and_install_your_module\n";
  107. print "makepack-dolibarrmodule.pl aborted.\n";
  108. sleep 2;
  109. exit 2;
  110. }
  111. # Get version $MAJOR, $MINOR and $BUILD
  112. print "Version detected for module ".$PROJECT.": ";
  113. $result=open(IN,"<".$SOURCE."/htdocs/".$PROJECTLC."/core/modules/mod".$PROJECT.".class.php");
  114. $custom=false;
  115. if (! $result) {
  116. $result=open(IN,"<".$SOURCE."/htdocs/custom/".$PROJECTLC."/core/modules/mod".$PROJECT.".class.php");
  117. if (! $result) {
  118. die "Error: Can't open descriptor file ".$SOURCE."/htdocs/(or /htdocs/custom/)".$PROJECTLC."/core/modules/mod".$PROJECT.".class.php for reading.\n";
  119. }else{
  120. $custom = true;
  121. }
  122. }
  123. while(<IN>)
  124. {
  125. if ($_ =~ /this->version\s*=\s*'([\d\.]+)'/) { $PROJVERSION=$1; break; }
  126. }
  127. close IN;
  128. print $PROJVERSION."\n";
  129. ($MAJOR,$MINOR,$BUILD)=split(/\./,$PROJVERSION,3);
  130. if ($MINOR eq '')
  131. {
  132. print "Enter value for minor version for module ".$PROJECT.": ";
  133. $MINOR=<STDIN>;
  134. chomp($MINOR);
  135. }
  136. $FILENAME="$PROJECTLC";
  137. $FILENAMETGZ="module_$PROJECTLC-$MAJOR.$MINOR".($BUILD ne ''?".$BUILD":"");
  138. $FILENAMEZIP="module_$PROJECTLC-$MAJOR.$MINOR".($BUILD ne ''?".$BUILD":"");
  139. if (-d "/usr/src/redhat") {
  140. # redhat
  141. $RPMDIR="/usr/src/redhat";
  142. }
  143. if (-d "/usr/src/RPM") {
  144. # mandrake
  145. $RPMDIR="/usr/src/RPM";
  146. }
  147. # Choose package targets
  148. #-----------------------
  149. $target="ZIP"; # Dolibarr modules are this format
  150. $CHOOSEDTARGET{uc($target)}=1;
  151. # Test if requirement is ok
  152. #--------------------------
  153. foreach my $target (keys %CHOOSEDTARGET) {
  154. foreach my $req (split(/[,\s]/,$REQUIREMENTTARGET{$target})) {
  155. # Test
  156. print "Test requirement for target $target: Search '$req'... ";
  157. $ret=`"$req" 2>&1`;
  158. $coderetour=$?; $coderetour2=$coderetour>>8;
  159. if ($coderetour != 0 && (($coderetour2 == 1 && $OS =~ /windows/ && $ret !~ /Usage/i) || ($coderetour2 == 127 && $OS !~ /windows/)) && $PROGPATH) {
  160. # Not found error, we try in PROGPATH
  161. $ret=`"$PROGPATH/$ALTERNATEPATH{$req}/$req\" 2>&1`;
  162. $coderetour=$?; $coderetour2=$coderetour>>8;
  163. $REQUIREMENTTARGET{$target}="$PROGPATH/$ALTERNATEPATH{$req}/$req";
  164. }
  165. if ($coderetour != 0 && (($coderetour2 == 1 && $OS =~ /windows/ && $ret !~ /Usage/i) || ($coderetour2 == 127 && $OS !~ /windows/))) {
  166. # Not found error
  167. print "Not found\nCan't build target $target. Requirement '$req' not found in PATH\n";
  168. $CHOOSEDTARGET{$target}=-1;
  169. last;
  170. } else {
  171. # Pas erreur ou erreur autre que programme absent
  172. print " Found ".$REQUIREMENTTARGET{$target}."\n";
  173. }
  174. }
  175. }
  176. print "\n";
  177. # Check if there is at least on target to build
  178. #----------------------------------------------
  179. $nboftargetok=0;
  180. $nboftargetneedbuildroot=0;
  181. $nboftargetneedcvs=0;
  182. foreach my $target (keys %CHOOSEDTARGET) {
  183. if ($CHOOSEDTARGET{$target} < 0) { next; }
  184. if ($target ne 'EXE' && $target ne 'EXEDOLIWAMP')
  185. {
  186. $nboftargetneedbuildroot++;
  187. }
  188. if ($target eq 'SNAPSHOT')
  189. {
  190. $nboftargetneedcvs++;
  191. }
  192. $nboftargetok++;
  193. }
  194. if ($nboftargetok) {
  195. # Update CVS if required
  196. #-----------------------
  197. if ($nboftargetneedcvs)
  198. {
  199. print "Go to directory $SOURCE\n";
  200. $olddir=getcwd();
  201. chdir("$SOURCE");
  202. print "Run cvs update -P -d\n";
  203. $ret=`cvs update -P -d 2>&1`;
  204. chdir("$olddir");
  205. }
  206. # Update buildroot if required
  207. #-----------------------------
  208. if ($nboftargetneedbuildroot)
  209. {
  210. if (! $copyalreadydone) {
  211. print "Delete directory $BUILDROOT\n";
  212. $ret=`rm -fr "$BUILDROOT"`;
  213. mkdir "$BUILDROOT";
  214. mkdir "$BUILDROOT/$PROJECTLC";
  215. $result=open(IN,"<makepack-".$PROJECT.".conf");
  216. if (! $result) { die "Error: Can't open conf file makepack-".$PROJECT.".conf for reading.\n"; }
  217. while(<IN>)
  218. {
  219. $entry=$_;
  220. if ($entry =~ /^#/) { next; } # Do not process comments
  221. $entry =~ s/\n//;
  222. if ($entry =~ /^!(.*)$/) # Exclude so remove file/dir
  223. {
  224. print "Remove $BUILDROOT/$PROJECTLC/$1\n";
  225. $ret=`rm -fr "$BUILDROOT/$PROJECTLC/"$1`;
  226. if ($? != 0) { die "Failed to delete a file to exclude declared into makepack-".$PROJECT.".conf file (Fails on line ".$entry.")\n"; }
  227. next;
  228. }
  229. $entry =~ /^(.*)\/[^\/]+/;
  230. print "Create directory $BUILDROOT/$PROJECTLC/$1\n";
  231. $ret=`mkdir -p "$BUILDROOT/$PROJECTLC/$1"`;
  232. if ($entry !~ /version\-/)
  233. {
  234. print "Copy $SOURCE/$entry into $BUILDROOT/$PROJECTLC/$entry\n";
  235. $ret=`cp -pr "$SOURCE/$entry" "$BUILDROOT/$PROJECTLC/$entry"`;
  236. if ($? != 0) { die "Failed to make copy of a file declared into makepack-".$PROJECT.".conf file (Fails on line ".$entry.")\n"; }
  237. }
  238. }
  239. close IN;
  240. @timearray=localtime(time());
  241. $fulldate=($timearray[5]+1900).'-'.($timearray[4]+1).'-'.$timearray[3].' '.$timearray[2].':'.$timearray[1];
  242. open(VF,">$BUILDROOT/$PROJECTLC/build/version-".$PROJECTLC.".txt");
  243. print "Create version file $BUILDROOT/$PROJECTLC/build/version-".$PROJECTLC.".txt with date ".$fulldate."\n";
  244. $ret=`mkdir -p "$BUILDROOT/$PROJECTLC/build"`;
  245. print VF "Version: ".$MAJOR.".".$MINOR.($BUILD ne ''?".$BUILD":"")."\n";
  246. print VF "Build : ".$fulldate."\n";
  247. close VF;
  248. }
  249. print "Clean $BUILDROOT\n";
  250. $ret=`rm -fr $BUILDROOT/$PROJECTLC/.cache`;
  251. $ret=`rm -fr $BUILDROOT/$PROJECTLC/.project`;
  252. $ret=`rm -fr $BUILDROOT/$PROJECTLC/.settings`;
  253. $ret=`rm -fr $BUILDROOT/$PROJECTLC/index.php`;
  254. $ret=`rm -fr $BUILDROOT/$PROJECTLC/build/html`;
  255. $ret=`rm -fr $BUILDROOT/$PROJECTLC/documents`;
  256. $ret=`rm -fr $BUILDROOT/$PROJECTLC/document`;
  257. $ret=`rm -fr $BUILDROOT/$PROJECTLC/htdocs/conf/conf.php.mysql`;
  258. $ret=`rm -fr $BUILDROOT/$PROJECTLC/htdocs/conf/conf.php.old`;
  259. $ret=`rm -fr $BUILDROOT/$PROJECTLC/htdocs/conf/conf.php.postgres`;
  260. $ret=`rm -fr $BUILDROOT/$PROJECTLC/htdocs/conf/conf*sav*`;
  261. if($custom){
  262. $ret=`cp -r $BUILDROOT/$PROJECTLC/htdocs/custom/* $BUILDROOT/$PROJECTLC/htdocs/.`;
  263. }
  264. $ret=`rm -fr $BUILDROOT/$PROJECTLC/htdocs/custom`;
  265. $ret=`rm -fr $BUILDROOT/$PROJECTLC/htdocs/custom2`;
  266. $ret=`rm -fr $BUILDROOT/$PROJECTLC/test`;
  267. $ret=`rm -fr $BUILDROOT/$PROJECTLC/Thumbs.db $BUILDROOT/$PROJECTLC/*/Thumbs.db $BUILDROOT/$PROJECTLC/*/*/Thumbs.db $BUILDROOT/$PROJECTLC/*/*/*/Thumbs.db $BUILDROOT/$PROJECTLC/*/*/*/*/Thumbs.db`;
  268. $ret=`rm -fr $BUILDROOT/$PROJECTLC/CVS* $BUILDROOT/$PROJECTLC/*/CVS* $BUILDROOT/$PROJECTLC/*/*/CVS* $BUILDROOT/$PROJECTLC/*/*/*/CVS* $BUILDROOT/$PROJECTLC/*/*/*/*/CVS* $BUILDROOT/$PROJECTLC/*/*/*/*/*/CVS*`;
  269. }
  270. # Build package for each target
  271. #------------------------------
  272. foreach my $target (keys %CHOOSEDTARGET) {
  273. if ($CHOOSEDTARGET{$target} < 0) { next; }
  274. print "\nBuild package for target $target\n";
  275. if ($target eq 'TGZ') {
  276. $NEWDESTI=$DESTI;
  277. if (-d $DESTI.'/../modules') { $NEWDESTI=$DESTI.'/../modules'; }
  278. print "Remove target $FILENAMETGZ.tgz...\n";
  279. unlink("$NEWDESTI/$FILENAMETGZ.tgz");
  280. print "Compress $BUILDROOT/* into $FILENAMETGZ.tgz...\n";
  281. $cmd="tar --exclude-vcs --exclude *.tgz --directory \"$BUILDROOT\" --mode=go-w --group=500 --owner=500 -czvf \"$FILENAMETGZ.tgz\" .";
  282. $ret=`$cmd`;
  283. if ($OS =~ /windows/i) {
  284. print "Move $FILENAMETGZ.tgz to $NEWDESTI/$FILENAMETGZ.tgz\n";
  285. $ret=`mv "$FILENAMETGZ.tgz" "$NEWDESTI/$FILENAMETGZ.tgz"`;
  286. }
  287. else
  288. {
  289. $ret=`mv "$FILENAMETGZ.tgz" "$NEWDESTI/$FILENAMETGZ.tgz"`;
  290. }
  291. next;
  292. }
  293. if ($target eq 'ZIP') {
  294. $NEWDESTI=$DESTI;
  295. if (-d $DESTI.'/../modules') { $NEWDESTI=$DESTI.'/../modules'; }
  296. print "Remove target $FILENAMEZIP.zip...\n";
  297. unlink "$NEWDESTI/$FILENAMEZIP.zip";
  298. print "Compress $FILENAMEZIP into $FILENAMEZIP.zip...\n";
  299. print "Go to directory $BUILDROOT/$PROJECTLC\n";
  300. $olddir=getcwd();
  301. chdir("$BUILDROOT/$PROJECTLC");
  302. $cmd= "7z a -r -tzip -mx $BUILDROOT/$FILENAMEZIP.zip *";
  303. print $cmd."\n";
  304. $ret= `$cmd`;
  305. chdir("$olddir");
  306. print "Move $FILENAMEZIP.zip to $NEWDESTI/$FILENAMEZIP.zip\n";
  307. $ret=`mv "$BUILDROOT/$FILENAMEZIP.zip" "$NEWDESTI/$FILENAMEZIP.zip"`;
  308. $ret=`chown $OWNER.$GROUP "$NEWDESTI/$FILENAMEZIP.zip"`;
  309. next;
  310. }
  311. if ($target eq 'EXE') {
  312. $NEWDESTI=$DESTI;
  313. if (-d $DESTI.'/../modules') { $NEWDESTI=$DESTI.'/../modules'; }
  314. print "Remove target $FILENAMEEXE.exe...\n";
  315. unlink "$NEWDESTI/$FILENAMEEXE.exe";
  316. print "Compress into $FILENAMEEXE.exe by $FILENAMEEXE.nsi...\n";
  317. $command="\"$REQUIREMENTTARGET{$target}\" /DMUI_VERSION_DOT=$MAJOR.$MINOR.$BUILD /X\"SetCompressor bzip2\" \"$SOURCE\\build\\exe\\$FILENAME.nsi\"";
  318. print "$command\n";
  319. $ret=`$command`;
  320. print "Move $FILENAMEEXE.exe to $NEWDESTI\n";
  321. rename("$SOURCE\\build\\exe\\$FILENAMEEXE.exe","$NEWDESTI/$FILENAMEEXE.exe");
  322. next;
  323. }
  324. }
  325. }
  326. print "\n----- Summary -----\n";
  327. foreach my $target (keys %CHOOSEDTARGET) {
  328. if ($CHOOSEDTARGET{$target} < 0) {
  329. print "Package $target not built (bad requirement).\n";
  330. } else {
  331. print "Package $target built successfully in $NEWDESTI\n";
  332. }
  333. }
  334. }
  335. if (! $batch) {
  336. print "\nPress key to finish...";
  337. my $WAITKEY=<STDIN>;
  338. }
  339. 0;