Skip to content

Instantly share code, notes, and snippets.

@soruma
Last active May 20, 2020 01:26
Show Gist options
  • Save soruma/a54bacf3ac8e6bff5049b84518e3ad88 to your computer and use it in GitHub Desktop.
Save soruma/a54bacf3ac8e6bff5049b84518e3ad88 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import fire
import subprocess
class SubprocessUtil(object):
def exec_popen(self, command):
proc = subprocess.Popen(
command,
shell = True,
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
stdout_data, stderr_data = proc.communicate()
print(stdout_data.decode())
print(stderr_data.decode())
class DockerCompose(object):
def u(self):
"""コンテナをデーモン起動"""
subprocess.run('docker-compose up -d'.split())
def d(self):
"""コンテナ終了"""
subprocess.run('docker-compose down'.split())
def r(self, *commands):
"""コンテナでコマンド実行"""
container = 'web'
subprocess.run(('docker-compose run --rm %s %s'
% (container, ' '.join(commands))).split())
class RailsExecute(object):
def clear(self):
"""データクリア"""
docker_compose = DockerCompose()
docker_compose.d()
docker_compose.r('rails', 'db:migrate:reset')
docker_compose.u()
self.dmi()
def dmi(self):
"""ダミーデータ入れ直し"""
DockerCompose().r('rails', 'dummy_data:import')
class Bundle(object):
def i(self):
"""bundle install"""
DockerCompose().r('bundle', 'install')
def g(self):
"""guard起動"""
DockerCompose().r('bundle', 'exec', 'guard')
def s(self):
"""spec実行"""
DockerCompose().r('bundle', 'exec', 'rspec')
class Pipelile(object):
def __init__(self):
self.dc = DockerCompose()
self.re = RailsExecute()
self.b = Bundle()
def r(self, *commands):
DockerCompose().r('rails', *commands)
def grep(self, search_word):
"""grepを行う railsフォルダ上の検索に不要なディレクトリ除去済み"""
grep_command = ['grep', '-rn', search_word, '.']
ignone = ['--exclude=*.min.*',
'--exclude=*~',
'--exclude=yarn-error.log',
'--exclude=yarn.lock',
'--exclude-dir=.git',
'--exclude-dir=_build',
'--exclude-dir=__snapshots__',
'--exclude-dir=coverage',
'--exclude-dir=deps',
'--exclude-dir=doc',
'--exclude-dir=log',
'--exclude-dir=node_modules',
'--exclude-dir=tmp',
'--exclude-dir=vendor'
]
subprocess.run(grep_command + ignone)
def tmsp(self):
"""git diff で差分があれば、更新時間を変更する"""
commands = ['git diff --name-only | xargs touch',
'git diff --cached --name-only | xargs touch']
for command in commands:
SubprocessUtil().exec_popen(command)
def gbd(self):
"""git の不要なブランチを削除する"""
subprocess.run(['git', 'fetch', '-p'])
command = 'git branch --merged | grep -vE "^\*|master$|develop$" | xargs -I % git branch -d %'
SubprocessUtil().exec_popen(command)
if __name__ == '__main__':
fire.Fire(Pipelile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment