Skip to content

Instantly share code, notes, and snippets.

@yamachu
Created January 30, 2017 06:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamachu/3cce558ee49e40a8c8ff7fd7da2793c7 to your computer and use it in GitHub Desktop.
Save yamachu/3cce558ee49e40a8c8ff7fd7da2793c7 to your computer and use it in GitHub Desktop.
dotnetのsdkのバージョンを変えるやつ
#!/usr/bin/env python3
import itertools
import sys
import subprocess
def reset_display_oneline():
print('\033[2A', file=sys.stderr)
print('\033[2K', file=sys.stderr)
print('\033[2A', file=sys.stderr)
def print_select_field(colums, column_max = 8, has_next = False, is_initialize = False):
if not is_initialize:
for _ in range(column_max + 2):
reset_display_oneline()
for i, col in enumerate(colums, 1):
if col is not None:
print(f'{i}. {col if col[0] != "_" else col[1:]}')
else:
print('')
print('9. restore all')
if has_next: print('0. next')
def switch_sdks_status(sdks, page_idx = -1, target = -1, restore = False, debug = False):
if (page_idx == -1 or target == -1) and not restore:
print('Not changed')
return
for outer, colmuns in enumerate(sdks):
for inner, sdk in enumerate(colmuns):
if sdk[0] == '_' and restore:
proc_enable = subprocess.run([
'sudo',
'mv',
'-niv' if debug else '',
f'/usr/local/share/dotnet/sdk/{sdk}',
f'/usr/local/share/dotnet/sdk/{sdk[1:]}'])
elif not restore and sdk[0] != '_' and (outer != page_idx or inner != target):
proc_disable = subprocess.run([
'sudo',
'mv',
'-niv' if debug else '',
f'/usr/local/share/dotnet/sdk/{sdk}',
f'/usr/local/share/dotnet/sdk/_{sdk}'])
elif not restore and sdk[0] == '_' and outer == page_idx and inner == target:
proc_enable = subprocess.run([
'sudo',
'mv',
'-niv' if debug else '',
f'/usr/local/share/dotnet/sdk/{sdk}',
f'/usr/local/share/dotnet/sdk/{sdk[1:]}'])
def routine():
proc_list_sdks = subprocess.run('ls /usr/local/share/dotnet/sdk',
shell=True,
stdout=subprocess.PIPE)
versions = [v for v in proc_list_sdks.stdout.decode('utf-8').strip().split()]
column_max = 8 if len(versions) > 8 else len(versions)
versions_list = [[f for f in feats] for feats in itertools.zip_longest(*[iter(versions)]*column_max)]
page_index = 0
print('Select with number key')
proc_current_version = subprocess.run('dotnet --version', shell=True, stdout=subprocess.PIPE)
print(f'Current sdk version is {proc_current_version.stdout.decode("utf-8").strip().split()[-1]}')
print_select_field(versions_list[page_index], column_max, len(versions_list) != 1, True)
while True:
try:
n = input()
except KeyboardInterrupt:
exit(130)
try:
num = int(n)
if num == 0:
page_index = (page_index + 1) % len(versions_list)
print_select_field(versions_list[page_index], column_max, len(versions_list) != 1)
elif 0 < num and num < column_max + 1:
selected_version = versions_list[page_index][num - 1]
switch_sdks_status(versions_list, page_index, num - 1, debug = True)
break
elif num == 9:
switch_sdks_status(versions_list, restore = True, debug = True)
break
else:
reset_display_oneline()
except ValueError:
reset_display_oneline()
if __name__ == '__main__':
routine()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment