CSSMinifier.pl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/perl
  2. #
  3. # CSSMinifer v1.0
  4. #
  5. # The MIT License (MIT)
  6. #
  7. # Copyright (c) 2014 Daniele Bonini
  8. #
  9. # Permission is hereby granted, free of charge, to any person obtaining a copy of
  10. # this software and associated documentation files (the "Software"), to deal in the
  11. # Software without restriction, including without limitation the rights to use,
  12. # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
  13. # Software, and to permit persons to whom the Software is furnished to do so, subject
  14. # to the following conditions:
  15. #
  16. # The above copyright notice and this permission notice shall be included in all
  17. # copies or substantial portions of the Software.
  18. #
  19. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  20. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  21. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  22. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  24. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. #
  26. use strict;
  27. use warnings;
  28. print "CSSMinifier v1.0\n";
  29. print "Copyright 2014 Daniele Bonini. All rights reserved.\n";
  30. print "Author: Daniele Bonini\n";
  31. print "Creation Date: Jun 27th, 2014\n";
  32. print "Started at ", scalar(localtime), "\n";
  33. # Initialization of the minifier components
  34. use CSS::Packer;
  35. my $CSSpacker = CSS::Packer->init();
  36. # Check of the arguments passed to the script
  37. if (! $ARGV[0]) {
  38. print "\n\n";
  39. print "Invalid argument, source file is required!";
  40. print "\n";
  41. exit 1;
  42. }
  43. my $sourcefile = $ARGV[0];
  44. my $destinationfile;
  45. if ($ARGV[1]) {
  46. $destinationfile = $ARGV[1];
  47. }
  48. else {
  49. $destinationfile = $sourcefile . "m";
  50. }
  51. print "Source file: \'", $sourcefile, "\'\n";
  52. print "Destination file: \'", $destinationfile, "\'\n";
  53. # Check if the source file exists
  54. if (! -T $sourcefile) {
  55. print "\n\n";
  56. print "Source file doesn't exist!";
  57. print "\n";
  58. exit 1;
  59. }
  60. # Check if the destinaton file exists
  61. if (-T $destinationfile) {
  62. my $replace="";
  63. while (($replace ne "Y") && ($replace ne "N")) {
  64. print "\n\n";
  65. print "Destination file \'$destinationfile\' already exists. Do you want to continue and replace it? (Y/N)\n";
  66. chomp($replace=<STDIN>);
  67. if (uc($replace) eq "N") {
  68. exit 0;
  69. }
  70. last if (uc($replace) eq "Y");
  71. }
  72. }
  73. my @sourcetexta;
  74. my $sourcetext;
  75. my $minifiedtext;
  76. # Read source file
  77. open(SFILEH, $sourcefile) || die "Error accessing the source file \'$sourcefile\': $!";
  78. if (! (@sourcetexta=<SFILEH>)) {
  79. die "Error reading the source file \'$sourcefile\': $!";
  80. }
  81. close(SFILEH);
  82. print "\n";
  83. print "Processing data..", "\n", "\n";
  84. # Map the array of the source text to a string scalar to align to the
  85. # packer->minify method definition requirements
  86. my $i;
  87. for( $i=0; $i<=$#sourcetexta; $i=$i+1 ) {
  88. $sourcetext.=$sourcetexta[$i];
  89. }
  90. # Execute the minification
  91. my $opts = {
  92. compress => "minify",
  93. no_compress_comment => 1,
  94. };
  95. $minifiedtext = $CSSpacker->minify( \$sourcetext, $opts );
  96. # Output the result to the console
  97. print $minifiedtext, "\n";
  98. # Write the result to the destination file..
  99. print "\n", "\n";
  100. print "Saving to \'" . $destinationfile . "\'...", "\n";
  101. print "\n", "\n";
  102. open(DFILEH, ">$destinationfile") || die "Error creating the destination file \'$destinationfile\': $!";
  103. if (! print DFILEH $minifiedtext, "\n" ) {
  104. die "Error saving the resulting data to \'$destinationfile\': $!";
  105. }
  106. close(DFILEH);
  107. # Ending..
  108. print "\n", "\n";
  109. print "OK, done!";
  110. exit 0;