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

Chore: Migrating from using the cordova-res package to using the capacitor-assets package for asset generation #2364

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

kwAsant
Copy link
Collaborator

@kwAsant kwAsant commented Aug 13, 2024

PR Checklist

  • PR title descriptive (can be used in release notes)

Description

  • The capacitor-assets library was added to package.json using yarn.
  • Added commands for generating splash images and icons within android.ts and android.workflow.ts for Android.
  • Added commands for generating splash images and icons within ios.ts and ios.workflow.ts for iOS
  • Due to the nature of the capacitor-assets package, splash image and icon generation can co-occur using new commands.
  • Updated the config.ts and deployment.model.ts files to incorporate an asset path for both android and ios.
  • Updated ios.ts and android.ts has been enabled to support legacy.
  • It is important to note that the command, yarn workplace android configure must be used before running the new commands.

Git Issues

Closes #2072

Screenshots/Videos

It is recommended that you watch these videos in full-screen

Android:

android.yarn.commands.mov

iOS:

ios.yarn.commands.mov

…ing the ➤ YN0000: ┌ Resolution step

➤ YN0000: └ Completed in 0s 259ms
➤ YN0000: ┌ Fetch step
➤ YN0000: └ Completed in 0s 607ms
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed in 0s 769ms
➤ YN0000: Done in 1s 717ms command. Solved by chnaging the  to that of the default that the terminal runs from (which is the  directory).
Updated  to allow for new  to be created.
Changed commands in  to allow for iOS app launcher icons and splash screens to be generated.
Changed commands in  to allow for Android app launcher icons and splash screens to be generated with updated  and legacy methods.
Copy link
Collaborator

@jfmcquade jfmcquade left a comment

Choose a reason for hiding this comment

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

Good job figuring this out. I've added quite a few comments (mostly minor) inline.

Also, you should remove the now unused cordova-res package via yarn remove, and delete any imports that may still exist.

packages/data-models/deployment.model.ts Outdated Show resolved Hide resolved
packages/data-models/deployment.model.ts Outdated Show resolved Hide resolved
packages/data-models/deployment.model.ts Outdated Show resolved Hide resolved
packages/scripts/src/tasks/providers/android.ts Outdated Show resolved Hide resolved
packages/scripts/src/tasks/providers/android.ts Outdated Show resolved Hide resolved
const relativeAssetPath = path.relative(process.cwd(), assetPath).replace(/^(\.\.\/|\.\/)+/, "");

const cmd = `npx @capacitor/assets generate --assetPath ${relativeAssetPath} --ios`;
const cwd = process.cwd().replace("/packages/scripts", ""); // output will be: "/../../idems/open-app-builder/packages/scripts"
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Rather than doing a string replace, it would probably be better to use the path npm library to get the correct path – path.relative(from, to) might be what you need

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have found out that only relative paths work for the npx command, so I am attempting to find methods of converting the path to a relative one (without the /../..)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

With research ( and help with ChatGPT ), this may be the best way to get rid of leading dots in a directory.

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK. Line 68 is effectively just getting the relative path to the root directory, right? I think you can delete this line and instead import ROOT_DIR from packages/scripts/src/paths.ts (look at the file changes for this PR, you'll see that packages/scripts/src/tasks/providers/android.ts used to import ROOT_DIR). Then you should be able to just pass this value to the execSync() function below, like:

execSync(cmd, { stdio: "inherit", cwd: ROOT_DIR });

See also comment on line 65

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have made these changes, and they function the same.

packages/scripts/src/tasks/providers/android.ts Outdated Show resolved Hide resolved
});
}

const relativeAssetPath = path.relative(process.cwd(), assetPath).replace(/^(\.\.\/|\.\/)+/, "");
Copy link
Collaborator

Choose a reason for hiding this comment

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

is there a nicer way to do this, without the .replace() and regex? Potentially an existing method of path? It could be worth thinking if this whole logic of manipulating the path and then then executing the command relative to the cwd could be simplified (I'm not saying that it definitely can, but it's quite hard to follow currently)

Copy link
Collaborator

Choose a reason for hiding this comment

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

See comment on line 68 below. I think this line could also use the ROOT_DIR value instead of process.cwd(), and avoid doing the regex replacement. Try logging out ROOT_DIR and assetPath at this point, and see if there's a method(s) of path that could be used to combine them and give the output you want

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Made the change here too.

packages/workflows/src/android.workflows.ts Show resolved Hide resolved
},
],
},
// Generate iOS assets from source images (splash.png, icon.png and, optionally, icon-foreground.png and icon-background.png)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think icon.png should be icon-only.png instead

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am not too sure what this comment is specifically referencing. When I checked my assets, the image was called icon.png. However, I think any name changes can be solved by accessing the Google Drive.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I was going off the docs, which suggest that the source asset should be called "icon-only.png". So I think that's what this comment should specify as the name for the source file too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Okay will do

},
};
// Generate android assets
const assetDirPath = assetPath.includes(".png") ? path.dirname(assetPath) : assetPath;
Copy link
Collaborator

@jfmcquade jfmcquade Aug 21, 2024

Choose a reason for hiding this comment

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

Do we know whether the file has to be a .png? Either way, I think it would be better to check whether the path is to a file in a more general way. Have a look at these docs (we're already importing fs so can use its methods)

@@ -19,6 +19,20 @@ const childWorkflows: IDeploymentWorkflows = {
},
],
},
set_icons_and_splash_images: {
label:
"Generate app launcher icon and splash screen image from icon.png and splash.png asset respectively. Then if provided, it will generate adaptive icon from icon-foreground.png and icon-background.png. Copy generated files to relevant folders.",
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think again that this should be "icon-only.png" instead of "icon.png". And no harm in linking to the docs again... Also copy over this comment to corresponding Android equivalent

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think this one may have to be changed within the gdrive. The comment I have changed but the actual name itself might have to be changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Migrate package used for generating Android assets cordova-res->@capacitor/assets
2 participants