Skip to content

Commit

Permalink
chore(cli): update plugin generation and plugin template (#3241)
Browse files Browse the repository at this point in the history
Co-authored-by: Ian Keith <[email protected]>
  • Loading branch information
imhoffd and ikeith authored Jul 16, 2020
1 parent 5e84ce9 commit 97a5b9a
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 78 deletions.
102 changes: 58 additions & 44 deletions cli/src/tasks/new-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ export async function newPlugin(config: Config) {
{
type: 'input',
name: 'name',
message: 'Plugin NPM name (kebab-case):',
message: 'Plugin npm name (kebab-case. ex: capacitor-plugin-example):',
validate: requiredInput
},
{
type: 'input',
name: 'domain',
message: 'Plugin id (domain-style syntax. ex: com.example.plugin)',
message: 'Plugin id (domain-style syntax. ex: com.mycompany.plugins.example)',
validate: requiredInput
},
{
type: 'input',
name: 'className',
message: 'Plugin class name (ex: AwesomePlugin)',
message: 'Plugin class name (ex: Example)',
validate: requiredInput
},
{
Expand Down Expand Up @@ -167,24 +167,24 @@ async function createIosPlugin(config: Config, pluginPath: string, domain: strin
}

function generatePodspec(config: Config, answers: NewPluginAnswers) {
return `
require 'json'
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
Pod::Spec.new do |s|
s.name = '${fixName(answers.name)}'
s.version = package['version']
s.summary = package['description']
s.license = package['license']
s.homepage = package['repository']['url']
s.author = package['author']
s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
s.source_files = 'ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}'
s.ios.deployment_target = '${config.ios.minVersion}'
s.dependency 'Capacitor'
s.swift_version = '5.0'
end`;
return `require 'json'
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
Pod::Spec.new do |s|
s.name = '${fixName(answers.name)}'
s.version = package['version']
s.summary = package['description']
s.license = package['license']
s.homepage = package['repository']['url']
s.author = package['author']
s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
s.source_files = 'ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}'
s.ios.deployment_target = '${config.ios.minVersion}'
s.dependency 'Capacitor'
s.swift_version = '5.1'
end
`;
}

async function createAndroidPlugin(config: Config, pluginPath: string, domain: string, className: string) {
Expand Down Expand Up @@ -221,47 +221,61 @@ function generateAndroidManifest(domain: string, pluginPath: string) {

function generatePackageJSON(answers: NewPluginAnswers, cliVersion: string) {
return {
name: answers.name,
version: '0.0.1',
description: answers.description,
main: 'dist/esm/index.js',
types: 'dist/esm/index.d.ts',
scripts: {
'build': 'npm run clean && tsc',
'name': answers.name,
'version': '0.0.1',
'description': answers.description,
'main': 'dist/plugin.js',
'module': 'dist/esm/index.js',
'types': 'dist/esm/index.d.ts',
'scripts': {
'lint': 'npm run prettier -- --check && npm run swiftlint -- lint',
'prettier': 'prettier "**/*.{css,html,ts,js,java}"',
'swiftlint': 'node-swiftlint',
'build': 'npm run clean && tsc && rollup -c rollup.config.js',
'clean': 'rimraf ./dist',
'watch': 'tsc --watch',
'prepublishOnly': 'npm run build'
},
author: answers.author,
license: answers.license,
dependencies: {
'@capacitor/core': `^${cliVersion}`
},
devDependencies: {
'rimraf': '^3.0.0',
'typescript': '^3.2.4',
'author': answers.author,
'license': answers.license,
'devDependencies': {
'@capacitor/android': `^${cliVersion}`,
'@capacitor/core': `^${cliVersion}`,
'@capacitor/ios': `^${cliVersion}`,
'@capacitor/android': `^${cliVersion}`
'@ionic/prettier-config': '^1.0.0',
'@ionic/swiftlint-config': '^1.0.0',
'@rollup/plugin-node-resolve': '^8.1.0',
'prettier': '^2.0.5',
'prettier-plugin-java': '^0.8.0',
'rimraf': '^3.0.0',
'rollup': '^2.21.0',
'swiftlint': '^1.0.1',
'typescript': '~3.8.3'
},
'peerDependencies': {
'@capacitor/core': `^${cliVersion}`
},
files: [
'files': [
'dist/',
'ios/',
'android/',
`${fixName(answers.name)}.podspec`
],
keywords: [
'keywords': [
'capacitor',
'plugin',
'native'
],
capacitor: {
ios: {
src: 'ios',
'capacitor': {
'ios': {
'src': 'ios',
},
android: {
src: 'android'
'android': {
'src': 'android'
}
},
'prettier': '@ionic/prettier-config',
'swiftlint': '@ionic/swiftlint-config',
'repository': {
'type': 'git',
'url': answers.git
Expand Down
2 changes: 2 additions & 0 deletions plugin-template/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
dist
4 changes: 2 additions & 2 deletions plugin-template/android/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;

@NativePlugin()
@NativePlugin
public class CLASS_NAME extends Plugin {

@PluginMethod()
@PluginMethod
public void echo(PluginCall call) {
String value = call.getString("value");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package com.getcapacitor.android;

import android.content.Context;
import static org.junit.Assert.*;

import androidx.test.platform.app.InstrumentationRegistry;
import android.content.Context;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import androidx.test.platform.app.InstrumentationRegistry;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {

@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.getcapacitor;

import org.junit.Test;

import static org.junit.Assert.*;

import org.junit.Test;

/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {

@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}
}
2 changes: 1 addition & 1 deletion plugin-template/ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Capacitor
*/
@objc(CLASS_NAME)
public class CLASS_NAME: CAPPlugin {

@objc func echo(_ call: CAPPluginCall) {
let value = call.getString("value") ?? ""
call.success([
Expand Down
16 changes: 8 additions & 8 deletions plugin-template/ios/PluginTests/PluginTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@ import Capacitor
@testable import Plugin

class PluginTests: XCTestCase {

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}

func testEcho() {
// This is an example of a functional test case for a plugin.
// Use XCTAssert and related functions to verify your tests produce the correct results.

let value = "Hello, World!"
let plugin = MyPlugin()

let call = CAPPluginCall(callbackId: "test", options: [
"value": value
], success: { (result, call) in
], success: { (result, _) in
let resultValue = result!.data["value"] as? String
XCTAssertEqual(value, resultValue)
}, error: { (err) in
}, error: { (_) in
XCTFail("Error shouldn't have been called")
})

plugin.echo(call!)
}
}
2 changes: 1 addition & 1 deletion plugin-template/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ end

target 'PluginTests' do
capacitor_pods
end
end
19 changes: 13 additions & 6 deletions plugin-template/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import nodeResolve from 'rollup-plugin-node-resolve';
import nodeResolve from '@rollup/plugin-node-resolve';

export default {
input: 'dist/esm/index.js',
output: {
file: 'dist/plugin.js',
format: 'iife',
name: 'capacitorPlugin',
sourcemap: true
name: 'capacitorPlugin', // TODO: change this
globals: {
'@capacitor/core': 'capacitorExports',
},
sourcemap: true,
},
plugins: [
nodeResolve()
]
};
nodeResolve({
// allowlist of dependencies to bundle in
// @see https://github.com/rollup/plugins/tree/master/packages/node-resolve#resolveonly
resolveOnly: ['lodash'],
}),
],
};
4 changes: 2 additions & 2 deletions plugin-template/src/definitions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
declare module "@capacitor/core" {
declare module '@capacitor/core' {
interface PluginRegistry {
Echo: EchoPlugin;
}
}

export interface EchoPlugin {
echo(options: { value: string }): Promise<{value: string}>;
echo(options: { value: string }): Promise<{ value: string }>;
}
2 changes: 1 addition & 1 deletion plugin-template/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './definitions';
export * from './web';
export * from './web';
4 changes: 2 additions & 2 deletions plugin-template/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ export class MyPluginWeb extends WebPlugin implements MyPluginPlugin {
constructor() {
super({
name: 'MyPlugin',
platforms: ['web']
platforms: ['web'],
});
}

async echo(options: { value: string }): Promise<{value: string}> {
async echo(options: { value: string }): Promise<{ value: string }> {
console.log('ECHO', options);
return options;
}
Expand Down
8 changes: 5 additions & 3 deletions plugin-template/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"allowUnreachableCode": false,
"declaration": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"lib": [
"dom",
"es2015"
],
"module": "es2015",
"moduleResolution": "node",
"noImplicitAny": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "dist/esm",
"pretty": true,
"sourceMap": true,
"strict": true,
"target": "es2015"
},
"files": [
Expand Down

0 comments on commit 97a5b9a

Please sign in to comment.