Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove readonly attribute for output path deletion #815

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .vsts-ci-windows-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ jobs:
version: 8.0.100
includePreviewVersions: true

- pwsh: |
attrib +r "$(build.sourcesdirectory)/src" /s /d
displayName: Set all repo files as readonly

- pwsh: |
cd $(build.sourcesdirectory)/src/Uno.Wasm.Bootstrap
dotnet msbuild /r /p:Configuration=Release /p:DISABLE_CLIHOST_NET6=true
Expand Down
30 changes: 29 additions & 1 deletion src/Uno.Wasm.Bootstrap/Extensions/PathHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;

namespace Uno.Wasm.Bootstrap.Extensions;

Expand All @@ -13,4 +14,31 @@ private static readonly char OtherDirectorySeparatorChar
public static string FixupPath(string path)
=> path.Replace(OtherDirectorySeparatorChar, Path.DirectorySeparatorChar);

/// <summary>
/// Recursively deletes a path, including files with the readonly attribute
/// </summary>
public static void DeleteDirectory(string path)
{
if (Directory.Exists(path))
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
// Some files may have been copied over from the source
// files with a readonly attribute, let's remove it before deleting

foreach (var file in Directory.GetFiles(path, "*", SearchOption.AllDirectories))
{
var attributes = File.GetAttributes(file);

if ((attributes & FileAttributes.ReadOnly) != 0)
{
File.SetAttributes(file, attributes & ~FileAttributes.ReadOnly);
}
}
}

// Delete all files and folders recursively
Directory.Delete(path, true);
}
}
}
43 changes: 43 additions & 0 deletions src/Uno.Wasm.Bootstrap/RemoveDirTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// ******************************************************************
// Copyright � 2015-2022 Uno Platform inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ******************************************************************
//
// This file is based on the work from https://github.com/praeclarum/Ooui
//
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Uno.Wasm.Bootstrap.Extensions;

namespace Uno.Wasm.Bootstrap;

public class RemoveDirTask_v0 : Microsoft.Build.Utilities.Task
{
[Required]
public string Path { get; private set; } = "";

public override bool Execute()
{
var fixedPath = PathHelper.FixupPath(Path);
PathHelper.DeleteDirectory(fixedPath);

return true;
}
}
13 changes: 8 additions & 5 deletions src/Uno.Wasm.Bootstrap/ShellTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ private void RunPackager()

if (Directory.Exists(workAotPath))
{
Directory.Delete(workAotPath, true);
DirectoryDelete(workAotPath);
}

DirectoryCreateDirectory(workAotPath);
Expand Down Expand Up @@ -988,7 +988,7 @@ private void RunPackager()

if (Directory.Exists(linkerInput))
{
Directory.Delete(linkerInput, true);
DirectoryDelete(linkerInput);
}

Directory.Move(_managedPath, linkerInput);
Expand Down Expand Up @@ -1579,7 +1579,7 @@ IEnumerable<byte> ComputeHash(string file)

if (Directory.Exists(_distPath))
{
Directory.Delete(_distPath, true);
DirectoryDelete(_distPath);
}

try
Expand All @@ -1606,6 +1606,9 @@ IEnumerable<byte> ComputeHash(string file)
TryObfuscateAssemblies(Path.Combine(_finalPackagePath, Path.GetFileName(_managedPath)));
}

private void DirectoryDelete(string path)
=> PathHelper.DeleteDirectory(path);

private static void MoveFileSafe(string source, string target)
{
if (File.Exists(source) && source != target)
Expand Down Expand Up @@ -1688,12 +1691,12 @@ private void CreateDist()

if (Directory.Exists(_workDistPath))
{
Directory.Delete(_workDistPath, true);
DirectoryDelete(_workDistPath);
}

if (Directory.Exists(_workDistRootPath))
{
Directory.Delete(_workDistRootPath, true);
DirectoryDelete(_workDistRootPath);
}

Log.LogMessage($"Creating managed path {_managedPath}");
Expand Down
8 changes: 1 addition & 7 deletions src/Uno.Wasm.Bootstrap/build/Uno.Wasm.Bootstrap.targets
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,7 @@
<WasmShellDistPath Condition="'$(WasmShellDistPath)'==''">$(OutputPath)/dist</WasmShellDistPath>
</PropertyGroup>

<ItemGroup>
<_DistFilesToDelete Include="$(OutputPath)dist\**" />
<_DistDirToDelete Include="$([System.IO.Directory]::GetDirectories(&quot;$(WasmShellDistPath)&quot;))" Condition="exists('$(WasmShellDistPath)')" />
</ItemGroup>

<Delete Files="@(_DistFilesToDelete)" />
<RemoveDir Directories="@(_DistDirToDelete)" />
<RemoveDirTask_v0 Path="$(WasmShellDistPath)" />
</Target>

<Target Name="_ValidateLegacyCLIPackage" BeforeTargets="CoreCompile">
Expand Down
Loading