Advertisement
bunam

create mac homebrew cask recipe for libreoffice lang pack

Nov 26th, 2016
834
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.37 KB | None | 0 0
  1. #!/usr/bin/env php
  2. <?php
  3.  
  4. // Copyright (c) 2016  https://github.com/bunam.
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms are permitted
  8. // provided that the above copyright notice and this paragraph are
  9. // duplicated in all such forms and that any documentation,
  10. // advertising materials, and other materials related to such
  11. // distribution and use acknowledge that the software was developed
  12. // by the <organization>. The name of the
  13. // <organization> may not be used to endorse or promote products derived
  14. // from this software without specific prior written permission.
  15. // THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16. // IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  
  19. // prupose : make homebrew cask recipes for all libreoffice languages in all in one script
  20. // see http://brew.sh
  21.  
  22. // WIP : https://github.com/caskroom/homebrew-cask/pull/27193#pullrequestreview-10240273
  23.  
  24. // after run, exemple for testing  :  brew cask reinstall libreoffice libreoffice-language-pack --language=fr
  25.  
  26. // keep nowdoc !!
  27. $LIBREOFFICE_LANGUAGE_ONE_TPL=<<<'END_OF_RUBY_TPL'
  28. cask 'libreoffice-language-pack' do
  29.   version '___VERSION____'
  30.   ___ALL_SELECTIONS___
  31.   # documentfoundation.org was verified as official when first introduced to the cask
  32.  url "http://download.documentfoundation.org/libreoffice/stable/#{version}/mac/x86_64/LibreOffice_#{version}_MacOS_x86-64_langpack_#{language}.dmg"
  33.  
  34.   name 'LibreOffice language pack'
  35.   homepage 'https://www.libreoffice.org/'
  36.   gpg "#{url}.asc", key_id: 'c2839ecad9408fbe9531c3e9f434a1efafeeaea3'
  37.  
  38.   depends_on cask: 'libreoffice'
  39.  
  40.   preflight do
  41.     system '/usr/bin/tar', '-C', "#{appdir}/LibreOffice.app/", '-xjf', "#{staged_path}/LibreOffice Language Pack.app/Contents/tarball.tar.bz2"
  42.     system '/usr/bin/touch', "#{appdir}/LibreOffice.app/Contents/Resources/extensions"
  43.   end
  44. end
  45. END_OF_RUBY_TPL;
  46.  
  47. function px_libreoffice_laguages_pack_make_list_and_sha256($LIBREOFFICE_VERSION,$SITE_BASE,$MATCH) {
  48.  
  49.     // get page
  50.     $html= file_get_contents($SITE_BASE);
  51.     if($html==false) die("Error getting : ".$SITE_BASE.chr(10)) ;
  52.  
  53.     // prepare for exploration
  54.     $dom = new DOMDocument();
  55.     $dom->loadHTML($html);
  56.     $items = $dom->getElementsByTagName('a');
  57.  
  58.     // all languages
  59.     $languages=array() ;
  60.  
  61.     foreach($items as $value) {
  62.         $attrs = $value->attributes;
  63.         //echo $value->nodeValue ."|".$attrs->getNamedItem('href')->nodeValue . chr(10);
  64.         $testMATCHStart=substr($value->nodeValue,0,$MATCH['length']) ;
  65.         $testMATCHEnd=substr($value->nodeValue,-4) ;
  66.         $testKeep=substr($value->nodeValue,$MATCH['length'],-4) ;
  67.         if($testMATCHStart==$MATCH['str'] && $testMATCHEnd==".dmg"){
  68.             // echo $value->nodeValue ."|".$testKeep.chr(10) ;
  69.             // dedouble !
  70.             $languages[$testKeep]=true ;
  71.         }
  72.     }
  73.  
  74.     // get SHA256
  75.     foreach($languages as $key => &$sha256 ){
  76.         //echo $key.chr(10);
  77.         $url = $SITE_BASE.$MATCH['str'].$key.'.dmg.sha256' ;
  78.         echo "Getting sha256 ".$url.chr(10);
  79.         sleep(1) ;
  80.         $html_sha256 = file_get_contents($url);
  81.         if($html_sha256==false) die("Error getting : ".$SITE_BASE.chr(10)) ;
  82.         $sha256 = substr($html_sha256,0,strpos($html_sha256," "));
  83.     }
  84.  
  85.     //print_r($languages) ;
  86.     // facility for debug
  87.     return file_put_contents(dirname(__FILE__)."/laguages.sha256.ser",serialize($languages)) ;
  88. }
  89.  
  90. function px_libreoffice_laguages_pack_make_cask_one_recipie($languages, $LIBREOFFICE_LANGUAGE_ONE_TPL, $LIBREOFFICE_VERSION, $PATH){
  91.    
  92.     $all_selections="" ;
  93.    
  94.     foreach($languages as $language => $sha256 ){
  95.    
  96.         if($language!='ca-valencia') {
  97.             // mini templating
  98.        
  99.             // defaulting
  100.             $default=" ";
  101.             if($language=="en-GB") {
  102.                 $default= ", default: true " ;
  103.             }
  104. # <- keep here     
  105. $all_selections .= "
  106.  language '".$language."'".$default."do
  107.    sha256 '".$sha256."'
  108.    '".$language."'
  109.  end
  110. " ;
  111.         }
  112.     }
  113.    
  114.     $newTPL=$LIBREOFFICE_LANGUAGE_ONE_TPL ;
  115.     $search  = array('___VERSION____', '___ALL_SELECTIONS___');
  116.     $replace = array($LIBREOFFICE_VERSION , $all_selections);
  117.     $filename=$PATH.'/libreoffice-language-pack.rb' ;
  118.     echo 'Writing '.$filename.chr(10) ;
  119.     file_put_contents($filename, str_replace($search, $replace, $newTPL));
  120.    
  121. }
  122.  
  123. # main
  124.  
  125. if(!isset($argv[1])) {
  126.     die("You must provide an argument as a version number ex : 5.2.3") ;
  127. }
  128.  
  129. $LIBREOFFICE_VERSION=$argv[1] ;
  130. $SITE_BASE='http://download.documentfoundation.org/libreoffice/stable/'.$LIBREOFFICE_VERSION.'/mac/x86_64/' ;
  131. $MATCH['str']="LibreOffice_".$LIBREOFFICE_VERSION."_MacOS_x86-64_langpack_" ;
  132. $MATCH['length']=strlen($MATCH['str']) ;
  133.  
  134. // where is brew ???
  135. exec('brew config | grep HOMEBREW_REPOSITORY',$output,$ret) ;
  136. if($ret==0) {
  137.     $HOMEBREW_REPOSITORY = substr($output[0],strpos($output[0]," ")+1) ;
  138.     $HOMEBREW_CASK_REPOSITORY = $HOMEBREW_REPOSITORY."/Library/Taps/caskroom/homebrew-cask/Casks" ;
  139.     if(!is_dir($HOMEBREW_CASK_REPOSITORY)) {
  140.         die("homebrew : cask not there ? : ".$HOMEBREW_CASK_REPOSITORY) ;
  141.     }
  142. }else{
  143.     die("homebrew not installed ?") ;
  144. }
  145.  
  146. px_libreoffice_laguages_pack_make_list_and_sha256($LIBREOFFICE_VERSION,$SITE_BASE,$MATCH) ;
  147.  
  148. $languages = unserialize(file_get_contents(dirname(__FILE__)."/laguages.sha256.ser")) ;
  149.  
  150. px_libreoffice_laguages_pack_make_cask_one_recipie($languages, $LIBREOFFICE_LANGUAGE_ONE_TPL, $LIBREOFFICE_VERSION, $HOMEBREW_CASK_REPOSITORY) ;
  151.  
  152.  
  153. echo "All finished".chr(10) ;
  154.  
  155. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement