Skip to content

Commit

Permalink
Merge pull request #3 from Lunik/develop
Browse files Browse the repository at this point in the history
Release 0.2.0
  • Loading branch information
Lunik authored Aug 6, 2023
2 parents d9d82cf + da954c0 commit ba0a8d4
Show file tree
Hide file tree
Showing 16 changed files with 522 additions and 147 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# CHANGELOG

## v0.2.0

### Features

- Upgrade the `git.setup` module :
- Add new aliases for `git` commands : `plr`, `rh`, `t`
- Add a new `lf` alias binary
- Display binary & module versions when printing the help message
- Add `macos` collection with `homebrew-setup`, `homebrew-upgrade`, `homebrew-cleanup` modules

### Changes

- Move the `laflem` script to a package "entry point"
- Move `git` collection constants outside of module files

### Fixes

- Add lint on `setup.py` file


## v0.1.2

### Fixes
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ pip install --user laflem

## Usage

To use LaFlem, simply run the following command:
To use LaFlem, simply run the following command :

```bash
laflem
laflem --help
```

or with the alias :

```bash
lf --help
```

## Contributing
Expand Down
7 changes: 3 additions & 4 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ tasks:
sources:
- setup.py
- lib/**/*
- scripts/**/*
- configs/**/*
generates:
- dist/**/*
Expand All @@ -115,14 +114,14 @@ tasks:
cmds:
- >
{{ .PYLINT_BIN }}
lib scripts
lib setup.py
|
tee
"{{ .CHECKS_DIR }}/pylint.result.txt"
sources:
- lib/**/*
- scripts/**/*
- tests/**/*
- setup.py
generates:
- "{{ .CHECKS_DIR }}/pylint.result.txt"

Expand Down Expand Up @@ -202,7 +201,7 @@ tasks:
cmds:
- >
{{ .PYTHON_BIN }}
scripts/laflem
-c "import sys; sys.argv[0] = '{{ .NAME }}'; from {{ .NAME }} import main; main()"
{{ .CLI_ARGS }}
run:
Expand Down
90 changes: 31 additions & 59 deletions lib/laflem/__init__.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,39 @@
'''
Root module for laflem
Main module for laflem
'''

import sys
import argparse

from laflem.exceptions import CollectionNotFound
from laflem.log import error_console

from .collections.base import BaseCollection
from .collections.git import GitCollection

__version__ = '0.1.2'
from .flem import Flem
from .exceptions import FlemException
from .log import error_console

class FlemParser(argparse.ArgumentParser):
'''
Custom python parser for laflem.
'''
def error(self, message):
'''
Print an error message and exit.
'''
error_console.print(f"error: {message}\n", style="bold red")
self.print_help()
sys.exit(2)
__version__ = '0.2.0'

class Flem:
def main():
'''
Root class for laflem.
Main function for laflem.
'''
collections = {
'base': BaseCollection,
'git': GitCollection,
}

def get_collection(self, name):
'''
Return an instance of the requested collection if it exists.
Else raise a CollectionNotFound exception.
'''
if name in self.collections:
return self.collections[name]()

raise CollectionNotFound(name)

@property
def parser(self):
'''
Construct the parser for laflem.
Returns an instance of FlemParser.
'''
parser = FlemParser(description='Process some integers.')

subparsers = parser.add_subparsers(help='collection', dest='collection')
subparsers.required = True
for collection in self.collections.values():
collection_parser = subparsers.add_parser(collection.name, help=collection.description)
collection.build_parser(collection_parser)

collection_subparsers = collection_parser.add_subparsers(help='module', dest='module')
collection_subparsers.required = True
for module in collection.modules.values():
module_parser = collection_subparsers.add_parser(module.name, help=module.description)
module.build_parser(module_parser)

return parser
flem = Flem(
version=__version__
)
args = flem.parser.parse_args()

try:
if args.collection:
collection = flem.get_collection(args.collection)
if args.module:
module = collection.get_module(args.module)

module.run(**args.__dict__)
sys.exit(0)

except Exception as error: # pylint: disable=broad-except
if isinstance(error, FlemException):
error_console.print(error, style="bold red")
else:
error_console.print_exception()

sys.exit(1)

if __name__ == '__main__':
main()
46 changes: 46 additions & 0 deletions lib/laflem/collections/git/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'''
Git module constants
'''

GIT_DEFAULT_CONFIG_PARAMS = [
('user', 'name', 'str', None),
('user', 'email', 'str', None),
('core', 'editor', 'str', 'vim'),
('core', 'pager', 'str', 'less'),
('core', 'autocrlf', 'str', 'input'),
('init', 'defaultBranch', 'str', 'master'),
('fetch', 'prune', 'bool', True),
('pull', 'rebase', 'bool', True),
('merge', 'ff', 'bool', True),
('push', 'autoSetupRemote', 'bool', True),
('push', 'rebase', 'str', 'preserve'),
]

GIT_GPP_CONFIG_PARAMS = [
('user', 'signingkey', 'str', None),
('commit', 'gpgsign', 'bool', True),
('tag', 'gpgsign', 'bool', True),
('push', 'gpgsign', 'bool', False),
('log', 'showSignature', 'bool', False),
('gpg', 'program', 'str', 'gpg2'),
]

GIT_ALIASES_PARAMS = [
('alias', 'co', 'str', "commit --signoff"),
('alias', 'lo', 'str', 'log'),
('alias', 'st', 'str', 'status'),
('alias', 'ph', 'str', 'push'),
('alias', 'pl', 'str', 'pull'),
('alias', 'a', 'str', 'add'),
('alias', 'df', 'str', 'diff'),
('alias', 'ck', 'str', 'checkout'),
('alias', 'mr', 'str', "merge --signoff"),
('alias', 'cp', 'str', "cherry-pick --signoff"),
('alias', 'br', 'str', 'branch'),
('alias', 'sh', 'str', 'stash'),
('alias', 'plh', 'str', '!git pull --rebase && git push'),
('alias', 'dp', 'str', '!git add . && git commit --signoff -m \'Dev\' && git push'),
('alias', 'plr', 'str', '!git pull --rebase origin master'),
('alias', 'rh', 'str', '!git reset --hard'),
('alias', 't', 'str', 'tag'),
]
46 changes: 5 additions & 41 deletions lib/laflem/collections/git/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
from laflem.collections.base.helloworld import HelloWorldModule

from laflem.tools.requirements import load_git_library
from .const import GIT_DEFAULT_CONFIG_PARAMS, GIT_GPP_CONFIG_PARAMS, GIT_ALIASES_PARAMS

class GitSetupModule(HelloWorldModule):
'''
The Git setup module.
'''
name = "setup"
description = "Init Git setup module."
version = "0.1.0"
version = "0.2.0"
extra_dependencies = 'git'

def __init__(self):
Expand Down Expand Up @@ -95,50 +96,13 @@ def _main(self, *_args, **kwargs):
console.print(f"Updating [bold green]{config_type}[/] Git configuration located in [bold blue]{config_path}[/]")
config = self.git.GitConfigParser(config_path, read_only=False)

params = [
('user', 'name', 'str', None),
('user', 'email', 'str', None),
('core', 'editor', 'str', 'vim'),
('core', 'pager', 'str', 'less'),
('core', 'autocrlf', 'str', 'input'),
('init', 'defaultBranch', 'str', 'master'),
('fetch', 'prune', 'bool', True),
('pull', 'rebase', 'bool', True),
('merge', 'ff', 'bool', True),
('push', 'autoSetupRemote', 'bool', True),
('push', 'rebase', 'str', 'preserve'),
]
params = GIT_DEFAULT_CONFIG_PARAMS

if Confirm.ask("Do you want to update the [bold green]GPG signature[/] config ?", default=False):
params += [
('user', 'signingkey', 'str', None),
('commit', 'gpgsign', 'bool', True),
('tag', 'gpgsign', 'bool', True),
('push', 'gpgsign', 'bool', False),
('log', 'showSignature', 'bool', False),
('gpg', 'program', 'str', 'gpg2'),
]
params += GIT_GPP_CONFIG_PARAMS

if Confirm.ask("Do you want to setup aliases ?", default=True):
signoff = Confirm.ask("Do all commits include a [bold green]SignOff[/] ?", default=True)
signoff_str = '--signoff' if signoff else ''

params += [
('alias', 'co', 'str', f"commit {signoff_str}"),
('alias', 'lo', 'str', 'log'),
('alias', 'st', 'str', 'status'),
('alias', 'ph', 'str', 'push'),
('alias', 'pl', 'str', 'pull'),
('alias', 'a', 'str', 'add'),
('alias', 'df', 'str', 'diff'),
('alias', 'ck', 'str', 'checkout'),
('alias', 'mr', 'str', f"merge {signoff_str}"),
('alias', 'cp', 'str', f"cherry-pick {signoff_str}"),
('alias', 'br', 'str', 'branch'),
('alias', 'sh', 'str', 'stash'),
('alias', 'plh', 'str', '!git pull --rebase && git push'),
('alias', 'dp', 'str', '!git add . && git commit --signoff -m \'Dev\' && git push'),
]
params += GIT_ALIASES_PARAMS

for section, key, valut_type, default_value in params:
self._update_git_config_value(config, section, key, valut_type, default_value)
24 changes: 24 additions & 0 deletions lib/laflem/collections/macos/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'''
Define the MacOS collection for Mac computers.
'''
from laflem.collections.base import BaseCollection

from .homebrew import HomebrewSetupModule, HomebrewUpgradeModule, HomebrewCleanupModule

class MacOSCollection(BaseCollection):
'''
The macos collection.
'''
name = "mac"
description = "The MacOS collection."
modules = {
"homebrew-setup": HomebrewSetupModule,
"homebrew-upgrade": HomebrewUpgradeModule,
"homebrew-cleanup": HomebrewCleanupModule,
}

@classmethod
def build_parser(cls, parser):
'''
Add arguments to the parser or options.
'''
6 changes: 6 additions & 0 deletions lib/laflem/collections/macos/homebrew/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'''
Import all modules in this package.
'''
from .setup import HomebrewSetupModule
from .upgrade import HomebrewUpgradeModule
from .cleanup import HomebrewCleanupModule
42 changes: 42 additions & 0 deletions lib/laflem/collections/macos/homebrew/cleanup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'''
Define the Homebrew cleanup module.
This module is used to cleanup Homebrew.
'''
import os
import subprocess
from rich.prompt import Confirm

from laflem.log import console
from laflem.collections.base.helloworld import HelloWorldModule
from laflem.exceptions.modules import ModuleNotRunnable

from .const import HOMEBREW_BIN_PATH

class HomebrewCleanupModule(HelloWorldModule):
'''
The Homebrew cleanup module.
'''
name = "homebrew-cleanup"
description = "Cleanup Homebrew."
version = "0.1.0"

def _check_installed(self):
'''
Check if Homebrew is installed.
'''
return os.path.exists(HOMEBREW_BIN_PATH)

def _main(self, *_args, **_kwargs):
'''
Core the module.
'''
if not self._check_installed():
raise ModuleNotRunnable("Homebrew is not installed.")

if Confirm.ask("Do you want to remove unused [bold blue]Homebrew[/] packages ?"):
console.print("Removing unused [bold blue]Homebrew[/] packages...")
subprocess.check_call([HOMEBREW_BIN_PATH, "autoremove"])

if Confirm.ask("Do you want to do a full cleanup of [bold blue]Homebrew[/] ?"):
console.print("Doing a full cleanup of [bold blue]Homebrew[/]...")
subprocess.check_call([HOMEBREW_BIN_PATH, "cleanup", "--prune=all"])
Loading

0 comments on commit ba0a8d4

Please sign in to comment.