Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
7078cf5
lint code via black
Zeroto521 Aug 10, 2021
7926099
lint codes via black
Zeroto521 Aug 10, 2021
a94b5bb
using pathlib to locate file
Zeroto521 Aug 10, 2021
bfb3768
lint codes via black
Zeroto521 Aug 10, 2021
bab45a1
lint codes via black
Zeroto521 Aug 10, 2021
893126a
separate project ignore and plugin ignore
Zeroto521 Aug 10, 2021
d0ca135
add blank line in code and description
Zeroto521 Aug 10, 2021
7dd2b29
simplify a bit inputting keyword
Zeroto521 Aug 10, 2021
92d0dd4
using f-string replace string concating
Zeroto521 Aug 10, 2021
d1728b9
add install dependencies command
Zeroto521 Aug 10, 2021
0ddfb9a
add clean command group
Zeroto521 Aug 10, 2021
e72ab28
add build command
Zeroto521 Aug 10, 2021
7863a8d
add setup environment command
jjw24 Aug 12, 2021
4d71e53
Merge remote-tracking branch 'origin/master' into development_updates
jjw24 Aug 12, 2021
0362866
EHN: add `get_build_ignores` func
Zeroto521 Aug 13, 2021
19cec9a
MAINT: gather all constants to one place
Zeroto521 Aug 13, 2021
01cb621
EHN: add `hook_env_snippet` func
Zeroto521 Aug 13, 2021
5091986
add `UrlDownload` and `UrlSourceCode` fields
Zeroto521 Aug 13, 2021
dbbdb8b
add field description + remove same keyword
Zeroto521 Aug 13, 2021
dd13448
use oop path replace fixed string
Zeroto521 Aug 13, 2021
873bae8
Create release.yml
Zeroto521 Aug 13, 2021
65ca68a
extract all TRANSLATIONS_PATH to one place
Zeroto521 Aug 13, 2021
a214108
should be `pip`
Zeroto521 Aug 13, 2021
8dcfe95
rename ci
Zeroto521 Aug 13, 2021
4e277fd
rename CI
Zeroto521 Aug 16, 2021
b6897ef
add plugin zip file constant
Zeroto521 Aug 16, 2021
4fb7ab5
import `PLUGIN_ZIP_NAME` from settings.py
Zeroto521 Aug 16, 2021
266cd0d
add single quotation to fix Linux env zip command can't ignore
Zeroto521 Aug 16, 2021
fd03d76
let hook env function return new entry script path
Zeroto521 Aug 16, 2021
6bb5217
Merge branch 'build-command' into development_updates
Zeroto521 Aug 16, 2021
63b51d3
merge env related command to env group
Zeroto521 Aug 16, 2021
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
8 changes: 0 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
# vscode
.history/
.vscode/


# python cache
__pycache__/


# mypy cache
.mypy_cache/
12 changes: 12 additions & 0 deletions Flow.Launcher.Plugin.PythonTemplate/.buildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# folder
.git/*
.github/*
.vscode/*
.history/*
*/__pycache__/*
build/*

# file
.gitignore
.gitattributes
.buildignore
68 changes: 68 additions & 0 deletions Flow.Launcher.Plugin.PythonTemplate/.github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Build and Publish

on:
push:
tags:
- "v*"

jobs:
build-publish:
name: Build and Publish
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: "3.x"

- name: Set up Environment
run: |
python -m pip install --upgrade pip
python commands.py setup-env

- name: Install Dependencies to Local
run: python commands.py setup-env-to-lib

- name: Compile all languages
run: python commands.py compile

- name: Update 'plugin.json' information
run: python commands.py gen-plugin-info

- name: Remove Python file artifacts
run: python commands.py clean-pyc

- name: Pack plugin to a zip file
run: python commands.py build

- name: Create GitHub Release
id: create_release
uses: actions/create-release@v1
env:
# This token is provided by Actions
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: ${{ github.ref }}
draft: false
prerelease: false

- name: Get Asset name
run: |
export PKG=$(ls build/ | grep zip)
set -- $PKG
echo "name=$1" >> $GITHUB_ENV

- name: Upload Release (zip) to GitHub
id: upload-release-asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: build/${{ env.name }}
asset_name: ${{ env.name }}
asset_content_type: application/zip
7 changes: 7 additions & 0 deletions Flow.Launcher.Plugin.PythonTemplate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# build
lib/
build/

# python cache
__pycache__/
.mypy_cache/
219 changes: 181 additions & 38 deletions Flow.Launcher.Plugin.PythonTemplate/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,94 @@

import json
import os
from textwrap import dedent
from typing import List

import click

from plugin import (ICON_PATH, PLUGIN_ACTION_KEYWORD, PLUGIN_AUTHOR,
PLUGIN_EXECUTE_FILENAME, PLUGIN_ID, PLUGIN_PROGRAM_LANG,
PLUGIN_URL, __long_description__, __package_name__,
__short_description__, __version__, basedir)
from plugin import (
ICON_PATH,
PLUGIN_ACTION_KEYWORD,
PLUGIN_AUTHOR,
PLUGIN_EXECUTE_FILENAME,
PLUGIN_ID,
PLUGIN_PROGRAM_LANG,
PLUGIN_URL,
PLUGIN_URL_DOWNLOAD,
PLUGIN_URL_SOURCE_CODE,
PLUGIN_ZIP_NAME,
TRANSLATIONS_PATH,
__long_description__,
__package_name__,
__short_description__,
__version__,
basedir,
)

# constants
# folder
build_path = basedir / "build"
build_path.mkdir(exist_ok=True)
lib_path = basedir / "lib"
lib_path.mkdir(exist_ok=True)

# file
build_ignore_path = basedir / ".buildignore"
build_ignore_path.touch() # if no existed, would be created
plugin_info_path = basedir / "plugin.json"
zip_path = build_path / f"{PLUGIN_ZIP_NAME}"

plugin_infos = {
"ID": PLUGIN_ID,
"ActionKeyword": PLUGIN_ACTION_KEYWORD,
"Name": __package_name__,
"Description": __short_description__,
"Author": PLUGIN_AUTHOR,
"Version": __version__,
"Language": PLUGIN_PROGRAM_LANG,
"Website": PLUGIN_URL,
"IcoPath": ICON_PATH,
"ExecuteFileName": PLUGIN_EXECUTE_FILENAME,
"UrlDownload": PLUGIN_URL_DOWNLOAD,
"UrlSourceCode": PLUGIN_URL_SOURCE_CODE,
}


def get_build_ignores(comment: str = "#") -> List[str]:
"""
Ignore file or folder when building a plugin, similar to '.gitignore'.
"""
ignore_list = []

with open(build_ignore_path, "r") as f:
for line in f.readlines():
line = line.strip()
if line and not line.startswith(comment):
ignore_list.append(line)

return ignore_list


def hook_env_snippet(exec_file: str = PLUGIN_EXECUTE_FILENAME) -> str:
"""Hook lib folder path to python system environment variable path."""

env_snippet = dedent(
f"""\
import os
import sys

basedir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(basedir, "{lib_path.name}"))
"""
)

temp_path = build_path / exec_file
entry_src = basedir / exec_file
with open(entry_src, "r") as f_r:
with open(temp_path, "w") as f_w:
f_w.write(env_snippet + f_r.read())

return temp_path


@click.group()
Expand All @@ -18,38 +99,39 @@ def translate():


@translate.command()
@click.argument('locale')
@click.argument("locale")
def init(locale):
"""Initialize a new language."""

if os.system('pybabel extract -F babel.cfg -k _l -o messages.pot .'):
raise RuntimeError('extract command failed')
if os.system('pybabel init -i messages.pot -d plugin/translations -l ' + locale):
raise RuntimeError('init command failed')
os.remove('messages.pot')
if os.system("pybabel extract -F babel.cfg -k _l -o messages.pot ."):
raise RuntimeError("extract command failed")
if os.system(f"pybabel init -i messages.pot -d {TRANSLATIONS_PATH} -l {locale}"):
raise RuntimeError("init command failed")
os.remove("messages.pot")

click.echo('Done.')
click.echo("Done.")


@translate.command()
def update():
"""Update all languages."""
if os.system('pybabel extract -F babel.cfg -k _l -o messages.pot .'):
raise RuntimeError('extract command failed')
if os.system('pybabel update -i messages.pot -d plugin/translations'):
raise RuntimeError('update command failed')
os.remove('messages.pot')
if os.system("pybabel extract -F babel.cfg -k _l -o messages.pot ."):
raise RuntimeError("extract command failed")
if os.system(f"pybabel update -i messages.pot -d {TRANSLATIONS_PATH}"):
raise RuntimeError("update command failed")
os.remove("messages.pot")

click.echo('Done.')
click.echo("Done.")


@translate.command()
def compile():
"""Compile all languages."""
if os.system('pybabel compile -d plugin/translations'):
raise RuntimeError('compile command failed')

click.echo('Done.')
if os.system(f"pybabel compile -d {TRANSLATIONS_PATH}"):
raise RuntimeError("compile command failed")

click.echo("Done.")


@click.group()
Expand All @@ -60,29 +142,90 @@ def plugin():

@plugin.command()
def gen_plugin_info():
"""Auto generate the `plugin.json` file for Flow"""
"""Auto generate the 'plugin.json' file for Flow."""

with open(plugin_info_path, "w") as f:
json.dump(plugin_infos, f, indent=4)

click.echo("Done.")


@plugin.command()
def build():
"Pack plugin to a zip file."

zip_path.unlink(missing_ok=True)

ignore_list = get_build_ignores()
ignore_string = "'" + "' '".join(ignore_list) + "'"
os.system(f"zip -r {zip_path} . -x {ignore_string}")

entry_src_hooked = hook_env_snippet()
os.system(f"zip -j {zip_path} {entry_src_hooked}")
entry_src_hooked.unlink()

click.echo("Done.")


@click.group()
def env():
...


@env.command()
def setup_env():
"""
Set up the environment for the first time.
This installs requirements.txt and requirements-dev.txt
"""

os.system("pip install -r requirements.txt --upgrade")
os.system("pip install -r requirements-dev.txt --upgrade")

click.echo("Environment ready to go.")


@env.command()
def setup_env_to_lib():
"""Install dependencies to local."""

os.system(f"pip install -r requirements.txt -t {lib_path} --upgrade")

click.echo("Done.")


@click.group()
def clean():
"""Clean commands."""
...


@clean.command()
def clean_build():
"""Remove build artifacts"""

os.system("rm -fr build/")
click.echo("Done.")

plugin_infos = {
"ID": PLUGIN_ID,
"ActionKeyword": PLUGIN_ACTION_KEYWORD,
"Name": __package_name__.title(),
"Description": __short_description__,
"Author": PLUGIN_AUTHOR,
"Version": __version__,
"Language": PLUGIN_PROGRAM_LANG,
"Website": PLUGIN_URL,
"IcoPath": ICON_PATH,
"ExecuteFileName": PLUGIN_EXECUTE_FILENAME
}

json_path = os.path.join(basedir, 'plugin.json')
with open(json_path, 'w') as f:
json.dump(plugin_infos, f, indent=' '*4)
@clean.command()
def clean_pyc():
"Remove Python file artifacts"

click.echo('Done.')
os.system(f"find {basedir} -name '*.pyc' -exec rm -f {{}} +")
Copy link
Member

Choose a reason for hiding this comment

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

Actually, the find command also doesn't exist for window.
I run it in ps env with choco to install these commands.

But those (ex. zip, find) commands will run at CI Linux env, so don't worry about it.

os.system(f"find {basedir} -name '*.pyo' -exec rm -f {{}} +")
os.system(f"find {basedir} -name '*~' -exec rm -f {{}} +")

click.echo("Done.")

cli = click.CommandCollection(sources=[plugin, translate])

if __name__ == "__main__":
cli = click.CommandCollection(
sources=[
clean,
env,
plugin,
translate,
]
)
cli()
23 changes: 18 additions & 5 deletions Flow.Launcher.Plugin.PythonTemplate/plugin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
# -*- coding: utf-8 -*-
"""
Plugin
=====
======
Introduce the plugin.
"""


from plugin.settings import (
GITHUB_USERNAME, ICON_PATH, PLUGIN_ACTION_KEYWORD, PLUGIN_AUTHOR,
PLUGIN_EXECUTE_FILENAME, PLUGIN_ID, PLUGIN_PROGRAM_LANG, PLUGIN_URL,
__long_description__, __package_name__, __short_description__, __version__,
basedir)
ICON_PATH,
PLUGIN_ACTION_KEYWORD,
PLUGIN_AUTHOR,
PLUGIN_EXECUTE_FILENAME,
PLUGIN_ID,
PLUGIN_PROGRAM_LANG,
PLUGIN_URL,
PLUGIN_URL_DOWNLOAD,
PLUGIN_URL_SOURCE_CODE,
PLUGIN_ZIP_NAME,
TRANSLATIONS_PATH,
__long_description__,
__package_name__,
__short_description__,
__version__,
basedir,
)
from plugin.ui import Main
Loading