2011-09-20 57 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"