2011-09-20 12 views
5

.jsファイルをまとめて圧縮し、古いものを同じフォルダ内の新しいものと置き換えるスクリプトを作成したいと考えています。私はいくつかのことを試しましたが、何か新しい問題に常に直面していることがわかったので、私がやり始めたことをよりよく理解している人に向けるのが最善であると思いました。PowerShellによる自動JavaScript圧縮

誰でも正しい方向に向けることができますか?

アップデート:私はこのようなコマンドのセットを使用しています :

>Get-ChildItem c:\NewFolder\ -recurse | 
&java -jar yuicompressor-2.4.6 

それはしかし、出力の使用のこれらの並べ替えを可能にしたいようですが、いないようです。私はこの作業を行う方法があると確信していますが、PowerShellにはまだまだ新しいものですが、私自身はまだ自信がありません。

更新: 以下の推奨コマンド文字列を使用すると、私は新しく圧縮された.jsから読み込まれたようなものを与えるためにpowershellを得ることができますが、既存のファイルを圧縮したものに置き換えたり、私は[filename] .min.js形式で同じディレクトリにあると信じている標準に。

更新: 推奨コマンドの変更されたバージョンがトリックを行うようです!

>Get-ChildItem c:\NewFolder\ -exclude jquery*.js,*min.js -recurse | %{java -jar yuicompressor-2.4.6.jar ($_.fullname) -o ($_.fullname)} 

しかし、PowerShellのコマンドを実行すると、奇妙なことに、私はJavaコマンドについてのPowerShellからエラーメッセージを取得しています...

のjava.exe:4文字:行: 72 + Get-ChildItem c:\ Scripts \ -exclude jquery * .js、* min.js -recurse | %{ジャワ< < < < -jar yuicompressor-2.4.6.jar($ .fullname) -o($ .fullname)} + CategoryInfo:NotSpecified:(:String)を[]、RemoteExceptionを + FullyQualifiedErrorId。 NativeCommandError使用方法:この情報 --typeを読む--charset入力ファイル の種類を指定し表示します。java -jar --help yuicompressor-xyzjar [オプション]、[入力ファイル]

グローバルオプション -h、入力ファイル を使用する - 改行指定した列番号の後に改行を挿入する -v、--verbos e情報メッセージと警告を表示します。 -o出力を置きます。デフォルトはstdoutです。 複数のファイルは、次の構文を使用して処理できます。 java -jar yuicompressor.jar -o '.css $: - min.css' * .css java -jar yuicompressor.jar -o '.js $: - min。 jsの*入力ファイルが指定されていない場合

JavaScriptのオプションnomunge縮小化が唯一、 難読化していない保存半は、それが標準入力に、すべてのセミコロンは 無効-最適化が

すべてのマイクロ最適化を無効にし、デフォルトを保存.jsファイル。この場合、「タイプ」オプションの が必要です。それ以外の場合は、入力ファイルの拡張子が 'js'でも 'css'でもない場合にのみ 'type'オプションが必要です( )。

PowerShellが私に教えようとしていることは何ですか?

答えて

5

次のようにやってみてください。

Get-ChildItem c:\NewFolder\ -recurse | %{java -jar yuicompressor-x.y.z.jar $_.fullname} 

%{..}foreach-objectの別名です。 c:\ Newfolder(およびそのサブディレクトリ)から一連のファイルを取得し、これらのファイルをオブジェクトとしてパイプラインの次のコンポーネントに渡します。この部分は、パイプラインもオブジェクトもサポートしていない外部コンポーネントであり、foreach内でラップし、ファイルのフルネーム(パスを含む)を理解できる形式で提供します。

+0

提案をいただきありがとうございます。間違いなく私を近づけますが、私はまだ正確にはありません。あなたのコマンドは各ファイルの圧縮を実行しているようですが、以前の圧縮されていないファイルや、[filename] .min.jsなどの別の名前でも出力​​.jsに書き込まれません。私は元の質問に上記の情報を追加します – ShowStopper

+1

あなたは実際に私が思ったよりもはるかに近づいている - あなたは本質的に私に答えを与えたmanojldsもう一度、非常にあなたの助けてくれてありがとう!私はあなたの名前を認識しているので、以前の私の質問ではあなたからの回答はありませんが、他の人の質問に単に答えるだけで、過去に私を助けてくれたことは間違いありません。また、ここであなたの役割をいただきありがとうございますstackoverflow.com。私はそれが頻繁に言われていないと確信していますが、私はあなたの助けがコミュニティによって非常に高く評価されたと思います。 – ShowStopper

+0

@ShowStopper - これがあなたの問題を解決した場合、答えとしてマークする必要があります(各回答で選択できるチェックマークがあります)。ボーナスポイントの場合は、同様に回答をアップすることもできます。 –

2

このスレッドは、あなたにいくつかの回答を提供するかもしれません。 What do you use to minimize and compress JavaScript libraries?

つまり、YUI compressorには、PowerShellから起動できるスタンドアロンの実行可能ファイルがあります。

+0

は、私は私が正しい方向に開始するため、この記事でチェイスFlorellに信用を与える必要があります私はPowerShellを使ってこれを行うことに近いですが、私は1つの重要な要素に問題があります。 – ShowStopper

1

私は最終的にこのコミュニティに何かを返すと思いました。私はPowershellを使ったことはないと言って始めて、本当にそれを嫌っています。私はbashに今Windowsが付属していることをうれしく思っています。しかし、私は、サーバーに勝つと彼のec2インスタンスに彼の.netアプリのコードを展開した後に実行するために何かを書く必要がある友人のための少し楽しいプロジェクトを持っていた。私は実際のビルドツールを使ってこれを行うための十数の良い方法について知っていますが、時にはスクリプトがあなたの望むものです。あなたはそれが役に立つと思うことを願っています。それはあなたがjavaをインストールし、クロージャージャーを持っている必要があります。他の小型化ツールを試すことができます。よく、これは間違いなくスタートで、YUIとGoogleの閉鎖コンパイラでプレーした後、私は感じて[OK]をHow to version javascript and css files as part of a powershell build process?

################################################################## 
# minimizeJS.ps1             # 
# This file removes all non-minified javascript resources  # 
# and then updates all html and aspx pages to point to the  # 
# minified js if they are not already.       # 
#                # 
# Version 1.0             # 
# Usage: minimizeJS.ps1 -debug true|false      # 
################################################################## 
param($debug=$true) #you can remove the =$true to force a user to specify 
$maxFiles = Get-ChildItem -Path ./* -Include *.js -Exclude *.min.js, *.min.comp.js -Recurse 
$filesContainingResourceRefs = Get-ChildItem -Path ./* -Include *.html, *.aspx -Recurse 
$fileCollection = New-Object System.Collections.ArrayList 

$closureJAR = 'C:\closure\compiler.jar' 
$javaLocation = 'C:\Program Files\Java\jre1.8.0_131\bin\java.exe' 

#Make sure debug flag is set one way or the other 
if (!$debug){ 
    Write-Host "Debug has not been set. Please use the -debug true|false argument." 
    Exit 
}elseif ($debug -eq $true){ 
    Write-host "Running with debug mode set to $debug." 
}elseif ($debug -eq $false){ 
    Write-host "Running wiht debug mode set to $debug." 
}else{ 
    Write-host "Debug has not been set properly. Please use the -debug true|false argument." 
    Exit 
} 

#First find everything we have a minified js version of, create an object of their names and paths, and delete the non-min file 
Write-Host "Beginning minification of JS files. Debug is $debug" 
foreach ($file in $maxFiles) 
    { 
     $fileOld = $file.FullName 
     $fileNew = $file.FullName.Replace(".js", ".min.js") 
     if ($debug -eq $true){ 
      #Write-Host java -jar $closureJAR --js $fileOld --js_output_file $fileNew 
      Write-Host " ArgList is: -jar $closureJAR --js $fileOld --js_output_file $fileNew" 
     }else{ 
      Write-Host " Minifying: $fileOld" 
      Start-Process -FilePath $javaLocation ` 
      -ArgumentList "-jar $closureJAR --js $fileOld --js_output_file $fileNew" ` 
      -RedirectStandardOutput '.\console.out' -RedirectStandardError '.\console.err' 
     } 
    } 
Write-Host "End minification of JS files" 


#Second find everything we have a minified js version of, create an object of their names and paths, and delete the non-min file 
Write-Host "Beginning Removal of files...will display below" 
$minFiles = Get-ChildItem -Path ./* -Filter *.min.js -Recurse 
foreach ($file in $minFiles) 
    { 
     #if ($file.FullName.Replace(".min.js", ".js") exists) { 
     $private:nonMinifiedVersionFull = $file.FullName -replace ".min.js", ".js" #.ToString().Replace(".min.js", ".js") 
     $private:nonMinifiedVersion = $file -Replace ".min.js", ".js" #.ToString().Replace(".min.js", ".js") 
     Write-Host " Removing: " $private:nonMinifiedVersion 
     if ($debug -eq $false) {Remove-Item $private:nonMinifiedVersionFull -ErrorAction SilentlyContinue} 

    $temp = New-Object System.Object 
    $temp | Add-Member -MemberType NoteProperty -Name "minFileName" -Value $file.ToString() 
    $temp | Add-Member -MemberType NoteProperty -Name "minFileFullName" -Value $file.FullName.ToString() 
    $temp | Add-Member -MemberType NoteProperty -Name "maxFileName" -Value $private:nonMinifiedVersion.ToString() 
    $temp | Add-Member -MemberType NoteProperty -Name "maxFileFullName" -Value $private:nonMinifiedVersionFull.ToString() 
    $fileCollection.Add($temp) | Out-Null 
    } 
Write-Host "End Removal of Files" 

if ($debug -eq $true) { 
    Write-Host "The fileCollection is:" 
    $fileCollection 
    } 

Write-Host "Beginning update of references to point to minified" 
#now go through all the files that could reference them and update the references 
foreach ($file2 in $filesContainingResourceRefs) 
    { 
     $private:file = $file2.FullName 

      $fixedContent = [System.IO.File]::ReadAllText($private:file) 

      #Now loop through all the min and max files in the collection 
      foreach ($line in $fileCollection) { 

        $strFind = $line.maxFileName.ToString() 
        $strReplace = $line.minFileName.ToString() 

        $fixedContent = $fixedContent.replace($strFind, $strReplace) 

      }    
      if ($debug -eq $false) {[System.IO.File]::WriteAllText($private:file, $fixedContent)} 


      Write-Host " Replaced non-minified references in: " $private:file 

    } 

Write-Host "End update of references to point to minified" 
関連する問題