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

Ionic capacitor hooks #4417

Merged
merged 18 commits into from May 11, 2020
Merged

Ionic capacitor hooks #4417

merged 18 commits into from May 11, 2020

Conversation

Heerschop
Copy link
Contributor

@Heerschop Heerschop commented May 3, 2020

This pull request contains changes to simplify usage of ionic capacitor from the CLI and in a CI/CD environment. By adding two extra hooks and by adding environment variables for all the hook shell scripts.

Added two ionic capacitor CLI Hooks

  • ionic:capacitor:run:before : executed on run before capacitor open is executed
  • ionic:capacitor:build:before : executed on build before capacitor open is executed

The capacitor hooks are executed after the ionic:serve and ionic:build hooks.

Updated the task run method. The task run method will now supply the shell scripts with generated environment variables based on the information in the Hook Input object.

Example usage

Content example script: build-before.sh

#!/bin/bash
echo Hook build-before.sh: $0 $*
env | grep IONIC_CLI_HOOK_CTX

Content example script: capacitor-build-before.sh

#!/bin/bash
echo Hook capacitor-build-before.sh: $0 $*
env | grep IONIC_CLI_HOOK_CTX

Content: package.json

{
  ...
  "scripts": {
    ...
    "ionic:build:before:after": "scripts/build-before.sh",
    "ionic:capacitor:build:before": "scripts/capacitor-build-before.sh"
  },
  ...
}

➜ test-cli-app git:(master) ✗ ionic capacitor build android

npm run ionic:build:before

test-cli-app@0.0.1 ionic:build:before /home/bas/Documents/temp/test-cli-app
scripts/build-before.sh

Hook build-before.sh: scripts/build-before.sh
IONIC_CLI_HOOK_CTX_NAME=build:before
IONIC_CLI_HOOK_CTX_BUILD_CORDOVA_ASSETS=true
IONIC_CLI_HOOK_CTX_BUILD_ENGINE=capacitor
IONIC_CLI_HOOK_CTX_BUILD_PLATFORM=android
IONIC_CLI_HOOK_CTX_BUILD_PROJECT=app
IONIC_CLI_HOOK_CTX_BUILD_TYPE=angular
IONIC_CLI_HOOK_CTX_BUILD_VERBOSE=false

ng run app:build
Generating ES5 bundles for differential loading...
ES5 bundle generation complete.

chunk {polyfills} polyfills-es2015.js, polyfills-es2015.js.map (polyfills) 297 kB [initial] [rendered]
....................
chunk {vendor} vendor-es5.js, vendor-es5.js.map (vendor) 5.39 MB [initial] [rendered]
Date: 2020-05-03T14:19:04.541Z - Hash: cd8ac09eeb78f0a5144a - Time: 9751ms

capacitor sync android
✔ Copying web assets from www to android/app/src/main/assets/public in 385.94ms
✔ Copying native bridge in 408.54μp
✔ Copying capacitor.config.json in 624.42μp
✔ copy in 397.01ms
✔ Updating Android plugins in 3.13ms
Found 0 Capacitor plugins for android:
✔ update android in 8.56ms
Sync finished in 0.414s
npm run ionic:capacitor:build:before

test-cli-app@0.0.1 ionic:capacitor:build /home/bas/Documents/temp/test-cli-app
scripts/capacitor-build-before.sh

Hook capacitor-build.sh: scripts/capacitor-build-before.sh
IONIC_CLI_HOOK_CTX_NAME=capacitor:build:before
IONIC_CLI_HOOK_CTX_BUILD_CORDOVA_ASSETS=true
IONIC_CLI_HOOK_CTX_BUILD_ENGINE=browser
IONIC_CLI_HOOK_CTX_BUILD_PROJECT=app
IONIC_CLI_HOOK_CTX_BUILD_TYPE=angular
IONIC_CLI_HOOK_CTX_BUILD_VERBOSE=false
IONIC_CLI_HOOK_CTX_CAPACITOR_APP_ID=io.ionic.starter
IONIC_CLI_HOOK_CTX_CAPACITOR_APP_NAME=test-cli-app
IONIC_CLI_HOOK_CTX_CAPACITOR_VERBOSE=false

[INFO] Ready for use in your Native IDE!

   To continue, build your project using Android Studio!

capacitor open android

packages/@ionic/cli/src/commands/capacitor/build.ts Outdated Show resolved Hide resolved
packages/@ionic/cli/src/definitions.ts Outdated Show resolved Hide resolved
packages/@ionic/cli/src/lib/hooks.ts Outdated Show resolved Hide resolved
@Heerschop
Copy link
Contributor Author

I'm not sure why these test are failing, the current develop branch is failing as well. I'm pretty sure it has nothing to do with the changes I have made

You are definitely right, i did not notice this bug when testing because lodash.snakeCase was fixing it
lodash.snakeCase('capacitor,server_url') => 'capacitor_server_url'

Co-authored-by: dwieeb <dwieeb@gmail.com>
@imhoffd
Copy link
Contributor

imhoffd commented May 8, 2020

I don't know why I suggested after, I think before makes more sense. I'm sorry 😂

The hook is technically being called before building the app or running it on a device. We call it before we open the IDE, so it should probably be before. 😬

Other than that, this is looking great!

@Heerschop
Copy link
Contributor Author

Ha ha, I agree and I have changed it to before :-)

@Heerschop
Copy link
Contributor Author

To make it complete I have added the capacitor:run:after and capacitor:build:after hook as well.

@Heerschop
Copy link
Contributor Author

Moved the [INFO] Ready for use in your Native IDE! message to after the invoke of the capacitor:run:before and capacitor:build:before hook.

The message log code is within the options['open'] if statement, so now when you use the --no-open option, the message will not been shown anymore.

@imhoffd
Copy link
Contributor

imhoffd commented May 9, 2020

I'm not sure if capacitor:run:after and capacitor:build:after should be included (yet). The Ionic CLI doesn't actually know when the app is run or built. If Capacitor ever gets a programmatic build, then the Ionic CLI could invoke it like it does for Cordova, in which case the hooks would make sense.

Thanks for fixing that other issue!

@Heerschop
Copy link
Contributor Author

It fine by me to remove or disable the capacitor:run:after and capacitor:build:after hooks. The use case it currently support is, doing pre and post processing when somebody is using the IDE (capacitor open). This might be a rare use case tho.

I have saved the work in a separate branch, and update this branch, so they are removed for now. If you changed your mind based on this comment, no problem let me know and I will restore them again :-)

@imhoffd
Copy link
Contributor

imhoffd commented May 11, 2020

The only thing capacitor open does is tell the OS to open the IDE. We don't know when they run the app or when they're done using it.

@imhoffd imhoffd merged commit 28d8540 into ionic-team:develop May 11, 2020
@imhoffd
Copy link
Contributor

imhoffd commented May 11, 2020

Awesome work @Heerschop! Thanks for the PR! I'll ship this ASAP.

@Heerschop
Copy link
Contributor Author

Thank you very much, it was nice working together.

@Heerschop Heerschop deleted the ionic-capacitor-hooks branch May 12, 2020 03:46
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.

None yet

2 participants