Why wont Shopify accept my theme's zip file when compressed using Powershell - powershell

I'm trying to create a workflow for designing multiple Shopify themes based on the same theme. I have written a simple PowerShell script that collects the necessary files and compresses them into a zip file.
Here is a simplified version of my script:
# Copy all files from the base theme to my temporary folder
Copy-Item $baseTheme"*" $tempFolder -recurse -force -exclude ".git"
# Include the files specific to the current theme
Copy-Item $specificTheme"assets" $tempFolder -recurse -force
Copy-Item $specificTheme"config" $tempFolder -recurse -force
Copy-Item $specificTheme"layout" $tempFolder -recurse -force
Copy-Item $specificTheme"snippets" $tempFolder -recurse -force
Copy-Item $specificTheme"templates" $tempFolder -recurse -force
# Compress the temporary folder
Compress-Archive $tempFolder $zipFileName
When I manually perform these steps, and create the zip file in Windows using Send To > Compressed (zipped) folder, Shopify is completely happy with the zip file.
However, when I upload the result of this script it gives me the following error:
There was 1 error:
zip does not contain a valid theme: missing template "layout/theme.liquid", missing template "templates/index.liquid", missing template "templates/collection.liquid", missing template "templates/product.liquid", missing template "templates/page.liquid", missing template "templates/cart.liquid", and missing template "templates/blog.liquid"
I've double and triple checked the zip file, and all of the required files exist. I've messed around with the -CompressionLevel of Compress-Archive. I've tried other methods of zipping folders within PowerShell. All with no luck.
I really can't see any difference between the results of the script and compressing manually. Any ideas?

I am answering my own question, but I'm not the one that solved it. I posted a link to this question on the shopify forum and someone named Adrian posted the following answer.
Short version. Download 7-ZIP and use the test archive facility. Zips
created with compress-archive show no folders whereas those created
with the Send To zip in Windows do. The folders do exist inside the
actual archive though so I'd say the header info is malformed.
Looks like there is a structural error within the files that
compress-archive creates that Windows is happy with but the, probably
Unix-based, Shopify servers don't accept.
I installed 7-Zip and used the Test Archive feature to evaluate both folders. Here is the (truncated) output:
Compressed with Compress-Archive
Archives: 1
Files: 77
There are no errors.
Compressed with Send-To Zip context menu
Archives: 1
Folders: 6
Files: 76
There are no errors.
At this point, I'm just happy knowing what's going on. But in order to really call this problem solved I updated my PowerShell script to use 7-Zip for compressing the folder.
I replaced this:
# Compress the temporary folder
Compress-Archive $tempFolder $zipFileName
With this:
# Compress the temporary folder
& "c:\Program Files\7-Zip\7z.exe" a -tzip $zipFile $tempFolder
I uploaded the archive to Shopify and everything worked just fine.
Now, I'm really not that excited about installing and running third party software to do what (I believe) should be a basic OS task. I've reported this issue to Microsoft here. Who knows, maybe they'll fix it.

This looks like an issue with the folder structure inside the archive file.
If you inspect the archive file created via PowerShell, does it show the same structure as the file created manually ?
This link shows the expected folder structure for a Shopify Theme:
index.liquid
product.liquid
collection.liquid
cart.liquid
blog.liquid
article.liquid
page.liquid
list_collections.liquid
search.liquid
404.liquid
gift_cards.liquid
customers/account.liquid
customers/activate.liquid
customers/addresses.liquid
customers/login.liquid
customers/order.liquid
customers/register.liquid
customers/reset_password.liquid
password.liquid
settings_schema.json
theme.liquid (layout file)

Related

Can I use PowerShell `Expand-Archive` upon a zip file with no extension

I'm working upon a PowerShell script, where I have to extract the content out of a .zip archive which extension is removed, so, archive's name is let's say not test.zip but just test, and it is compressed as a .zip archive.
I'm trying to use for this purpose the PowerShell cmdlet Expand-Archive like shown below :
Expand-Archive -LiteralPath "Path to the archive" -DestinationPath "Extraction Path"
But, it doesn't seem to work, is there a possibility of extracting this archive's content with powershell, or it would be better to use a work around like 7zip command line tools, or something similar?
The Expand-Archive cmdlet is designed to explicitly work with a path that has a .zip extension. You can work around this by either creating a copy of your archive with a proper extension using Copy-Item or renaming the archive to have an extension with Rename-Item (using Move-Item may be more desirable if the archive with extension already exists and you want to overwrite it; Rename-Item is not capable of overwriting).

Powershell copy file from an environment variable to the zip folder of another environment variable

I am really new to the Powershell and want to copy a file from BUILD_SOURCESDIRECTORY environment variable to a zip folder inside BUILD_STAGINGDIRECTORY environment variable in my VSTS build definition.
if(Test-Path $Env:BUILD_SOURCESDIRECTORY/MyFolder/MyFIle.txt)
{
Write-Host "Source File: $Env:BUILD_SOURCESDIRECTORY/MyFolder/MyFIle.txt"
Write-Host "Target Location: $Env:BUILD_STAGINGDIRECTORY\StagingDirectoryFolder.zip\TestFolder"
}
Copy file from one path to another is quite straight forward but I really don't know how to move file into the zip folder structure.
If I could suggest a non power shell solution (although it is worth looking up the Expand-Archive and Compress-Archive cmdlets as recommended in the comments.)
I would use a Archive Files build task to handle the zipping. In your power shell build script, copy your artifact(s) into $ENV:BUILD_BINARIESDIRECTORY, and then leverage the VSTS build to do the archiving of all of the files.
This then lets you publish that zip file using the VSTS build which will allow it to be easily accessible through the VSTS web gui which imo offers a superior user experience (for troubleshooting your build, as well as other users who need access to those artifacts (either physical people, or automated processes)). If you need to do something else with the zip file, you could then add another powershell script after your archive files that would be able to access the file from the $ENV:BUILD_ARTIFACTSTAGINGDIRECTORY. This way your scripts stay simple, and you can offload some of your build maintenance onto Microsoft.
You can copy MyFIle.txt to the subfolder TestFolder under a zip file by Expand-Archive and Compress-Archive (as gvee mentions). The PowerShell script as below:
if(Test-Path $(Build.SourcesDirectory)\MyFolder/MyFIle.txt)
{
clear-host
[string]$zipF = '$(Build.ArtifactStagingDirectory)\StagingDirectoryFolder.zip'
[string]$fileToZip = '$(Build.SourcesDirectory)\MyFolder\MyFIle.txt'
[string]$tempEx= '$(Build.SourcesDirectory)\temp'
[string]$copyDes='$(Build.SourcesDirectory)\temp\TestFolder'
Expand-Archive -Path $zipF -DestinationPath $tempEx -Force
Copy-Item $fileToZip -Destination $copyDes -Force
Compress-Archive -Path $tempEx\* -Update -DestinationPath $zipF
Remove-Item -Recurse -Force $tempEx
}
Now the zip file $(Build.ArtifactStagingDirectory)\StagingDirectoryFolder.zip contains MyFIle.txt under $(Build.ArtifactStagingDirectory)\StagingDirectoryFolder.zip\TestFolder.

Unable to delete file location using remove-item because it does not exist

I'm writing a script to be implemented in my desk to delete some files for Skype for Business.
I can get the Get-ChildItem command to find the folder but when I use the remove item it says that the file does not exist.
The code is
$SkypeFile = Get-ChildItem -Path c:\users\env:username\AppData\Local\Microsoft sip_* -Force -Recurse -Directory
Remove-Item $SkypeFile
The error code is:
Remove-item : Cannot find path "c:\New Folder..." (the file location I getfrom Get-ChildItem) because it doest not exist.
From what I understand it is searching in the folder that I have the script in and that why it is now working but I am unsure and stuck on why is it doing that.
EDIT:
I have managed to find out why was it looking in the script file location, I added the path but now I encountered a problem.
The file I am looking for can be in one of 3 folders titled 14.0 or 15.0 or 13.0 and I cannot make the remove file item look for it in those folders it just tries to look inside the folder.

Powershell 5.0 Compress-archive creates empty file duplicates of some folders

Long story short, I have a powershell script which compresses several folders into zip-files.
In order to compress a single directory into a zip file, I use this command:
Compress-Archive -Path $SourcePath -DestinationPath $OutputPath -CompressionLevel Optimal
Where $SourcePath is an absolute path ending on *, e.g. C:\Build\Output*, and $OutputPath is an absolute path ending on .zip, e.g. C:\Build\Debug.zip.
There are a lot of files and folders in the source path.
The issue I experience is that, scattered around the zip file, folders have a duplicate empty file. This causes problems when trying unzip the archive with e.g. 7-zip.
Interestingly enough, I do not see this issue with the build-in unzip in Total Commander.
I am wondering if this is an issue with the Powershell command, or 7-zip?

Powershell Write-Zip Partial Folder Tree

I have a folder structure that looks like this:
-Daily Logs
-Script1Logs
-AnotherScriptLogs
-ThirdScriptLogs
I would like to zip all the folders underneath "Daily Logs" and retain the same folder structure.
Is there a way to zip the folders in one shot without going through each one individually?
Also, can the zip file include only the file structure shown above instead of the entire path to the log folders?
This is what I am using to zip the Daily Logs folder, but it does not zip the sub folders and it includes the entire path to the daily logs folder as part of the folder structure inside the zip file.
If (!(Test-Path $ARCHIVELOGFOLDER\$ARCHIVELOGFILE)) {
Write-Zip $file.FullName $ARCHIVELOGFOLDER\$ARCHIVELOGFILE
Write-Zip -Path $files -OutputPath $ArchiveFile -Append -FlattenPaths -IncludeEmptyDirectories -Quiet -Level 9
}
Write-Zip is not a native PowerShell cmdlet. The one you're using looks like the PowerShell Community Extensions version written by Oisin. It will preserve paths and not use a full path if used like so:
C:\InetPub> Write-Zip -Path Logs -OutputPath logs.zip -IncludeEmptyDirectories
Note that there have been several bugs fixed in the latest release candidates for 2.1 and 3.0. You can pick up those versions here and here.