2011-09-20 62 views
5

我正在尋找一個腳本,將採取一堆.js文件,壓縮他們,然後用新的在同一個文件夾中替換舊的。我嘗試了一些東西,但是我發現自己不斷遇到新問題,所以我認爲最好轉向那些對我有更好理解並且開始新鮮的人。PowerShell自動JavaScript壓縮

任何人都可以指向正確的方向嗎?

更新: 我使用了一組類似這樣的命令:

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

它似乎並不像它希望讓這些類型的輸出使用的雖然。我確信有一種方法可以完成這項工作,但對於PowerShell來說還是相當新的,但我並不太自信,現在我可以自己搞清楚。

更新: 使用下面的建議命令字符串,我可以讓PowerShell給我什麼似乎是一個新的壓縮.js讀出,但它不會替換現有的文件與壓縮的一個或寫它到我認爲在[filename] .min.js格式的相同目錄中的標準。

更新: 建議的命令的修改版似乎有訣竅!

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

然而,當在PowerShell中的命令運行,奇怪的是,我正在從PowerShell中關於Java命令的錯誤消息...

的java.exe:在行:4字符: 72 + Get-ChildItem c:\ Scripts \ -exclude jquery * .js,* min.js -recurse | %{的java < < < <罐子的YUICompressor-2.4.6.jar($ .fullname) -o($ .fullname)} + CategoryInfo:NotSpecified:(:字符串)[],的RemoteException + FullyQualifiedErrorId: NativeCommandError用法:Java的罐子的YUICompressor-xyzjar [選項] [輸入文件]

全局選項 -h,--help顯示此信息 --type指定輸入文件 類型--charset閱讀輸入文件使用 --line-break在指定的列號後插入換行符 -v,--verbos e顯示參考消息和警告 -o將輸出放入。默認爲stdout。 可以使用以下語法處理多個文件: java -jar yuicompressor.jar -o'.css $: - min.css'* .css java -jar yuicompressor.jar -o'.js $: - min。 JS' * .js文件

的JavaScript選項nomunge縮減大小而已,不 模糊處理保持半保留所有分號 禁用的優化禁用所有微觀優化

如果沒有指定輸入文件,則默認爲標準輸入。在這種情況下,需要 'type'選項。否則,僅當輸入文件擴展名既不是'js'也不是'css'時,'type'選項才需要 。

任何想法PowerShell試圖告訴我什麼?

回答

5

嘗試做這樣的:

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

%{..}foreach-object別名。您從c:\ Newfolder(及其子分區)獲取一組文件,並將每個文件作爲對象傳遞給管道中的下一個組件。這部分是既不支持流水線操作也不支持對象的外部組件,您將它包裝在foreach中,並以它能夠理解的形式(文件的全名(包括路徑))提供文件。

+0

非常感謝您的建議 - 它絕對讓我接近,但我不完全在那裏。您的命令似乎爲每個文件運行壓縮,但不會將其寫入輸出.js,替換之前未壓縮的文件,或者甚至以不同的名稱(如[filename] .min.js)。我將在原始問題中添加更多信息 – ShowStopper

+1

您實際上讓我感覺比我想象的要緊密得多 - 您基本上給了我答案manojlds,非常感謝您的幫助!我承認你的名字,所以我有一種感覺,雖然我以前的任何問題都沒有你的答案,但你毫無疑問通過簡單地回答別人的問題來幫助我,也想感謝你在stackoverflow.com上的角色。我確信它經常沒有說出來,但是,我認爲你的幫助非常受到社區的讚賞。 – ShowStopper

+0

@ShowStopper - 如果這解決了您的問題(您可以通過每個答案選中一個複選標記),則應將此標記爲答案。對於獎勵積分,您也可以對答案進行投票。 –

1

所以,我想我最終會回饋給這個社區。我應該開始說我以前從未使用Powershell,並且真的不喜歡它。我很高興bash現在隨Windows一起發貨。但是,我爲在win服務器上運行的朋友開發了一個有趣的項目,並且需要編寫一些代碼將他的.net應用程序部署到他的ec2實例後才能運行。我知道大約有十幾種使用真實構建工具的更好的方法,但是......有時腳本就是你想要的。希望你們覺得它有用。它需要你安裝java並擁有關閉jar;你可以嘗試其他縮小工具。

我需要給予信貸大通Florell在這個職位讓我在正確的方向開始: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"