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

Feat: Copy several files to destination directory (optionally) #34

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 0 additions & 27 deletions Editor/IO/Files/CopyFileToDirectory.cs

This file was deleted.

40 changes: 40 additions & 0 deletions Editor/IO/Files/CopyFilesToDirectory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.IO;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;

namespace UniTools.Build
{
[CreateAssetMenu(
fileName = nameof(CopyFilesToDirectory),
menuName = MenuPaths.IO + nameof(CopyFilesToDirectory)
)]
public sealed class CopyFilesToDirectory : BuildStep
{
[SerializeField] private PathProperty[] m_filePaths = default;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can name it m_sourceDirectory

Copy link
Author

@MoxieWhimsy MoxieWhimsy May 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I will add a property for source directory. It may simplify using this build step. However I will also rename this to m_sourceFiles for clarity.

[System.Obsolete, HideInInspector]
[SerializeField] private PathProperty m_filePath = default;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like can be removed

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Since we won't be replacing CopyFileToDirectory, we don't need to replicate its properties.

[SerializeField] private PathProperty m_destination = default;

public override async Task Execute()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check is the m_sourceDirectory path is a directory path. If not - throw an exception

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I will make sure m_destinationDirectory is a directory path.

{
foreach (var filePath in m_filePaths)
{
string fileName = Path.GetFileName(filePath.ToString());
string dest = Path.Combine(m_destination.ToString(), fileName);
FileUtil.DeleteFileOrDirectory(dest);
FileUtil.CopyFileOrDirectory(filePath.ToString(), dest);
}

await Task.CompletedTask;
}

private void OnValidate()
MoxieWhimsy marked this conversation as resolved.
Show resolved Hide resolved
{
if (m_filePaths.Length == 0 && !string.IsNullOrEmpty(m_filePath.ToString()))
{
m_filePaths = new[] { m_filePath };
}
}
}
}