Skip to content

Commit 65aef5a

Browse files
committed
auto download: respect XDG convention on linux
there's also a function which automatically fixes up my stuff up now... More complexity for just not searching around a little bit. fix #15
1 parent baa8861 commit 65aef5a

File tree

1 file changed

+58
-7
lines changed

1 file changed

+58
-7
lines changed

server.py

+58-7
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ def start(cls):
5757
executable_path = settings.get(SETTINGS_EXECUTABLE)
5858
executable_path = os.path.expanduser(os.path.expandvars(executable_path))
5959

60-
if not os.path.exists(executable_path):
60+
if not os.path.exists(executable_path) and not fixed_wrong_location(
61+
executable_path
62+
):
63+
6164
if not confirm_automatic_download(executable_path):
6265
notify(
6366
"no binary found, and automatic updates disabled. JSONComma will not work"
@@ -365,15 +368,21 @@ def get_default_executable_path(cls, *, expand_vars):
365368
expand_vars, bool
366369
), "expand_vars should be a boolean, got {}".format(type(expand_vars))
367370

368-
path = {
369-
# sublime.platform() -> path for executable
370-
"windows": "%APPDATA%\\jsoncomma\\jsoncomma.exe",
371-
"linux": "~/.config/jsoncomma/jsoncomma",
372-
"osx": "~/Library/Application Support/jsoncomma/jsoncomma",
373-
}[sublime.platform()]
371+
path = None
372+
platform = sublime.platform()
373+
if platform == "windows":
374+
path = "%APPDATA%\\jsoncomma\\jsoncomma.exe"
375+
elif platform == "osx":
376+
path = "~/Library/Application Support/jsoncomma/jsoncomma"
377+
elif platform == "linux":
378+
xdg_data_home = os.environ.get("XDG_DATA_HOME", "") or "~/.local/share"
379+
path = os.path.join(xdg_data_home, "jsoncomma", "jsoncomma")
380+
381+
assert path is not None, "unknown platform {!r}".format(platform)
374382

375383
if expand_vars:
376384
return os.path.expandvars(os.path.expanduser(path))
385+
377386
return path
378387

379388

@@ -429,3 +438,45 @@ def kill_nicely(process, *, timeout=1):
429438
except subprocess.TimeoutExpired:
430439
process.kill()
431440
return process.poll()
441+
442+
443+
def fixed_wrong_location(new_path):
444+
""" I stuffed the location of the binary executable initially, and @WillMoggridge
445+
pointed me towards the relevant document and paths I should use.
446+
447+
So, this is an automatic fix for my mistake
448+
449+
This returns True if it did fix anything
450+
"""
451+
if sublime.platform() != "linux":
452+
return False
453+
454+
old_path = os.path.expanduser("~/.config/jsoncomma/jsoncomma")
455+
456+
if not os.path.isfile(old_path):
457+
return False
458+
459+
# we know what new_path doesn't exists because otherwise, this fixup wouldn't have been
460+
# called
461+
462+
try:
463+
os.makedirs(os.path.dirname(new_path), exist_ok=True)
464+
os.rename(old_path, new_path)
465+
# we shouldn't have any other files in old_path's folder. If we do, rmdir is going
466+
# to fail (which we want it to, don't want to remove files we haven't put there)
467+
os.rmdir(os.path.dirname(old_path))
468+
except OSError:
469+
import traceback
470+
471+
sublime.error_message(
472+
"Failed trying to automatically move {} to {}. Accept the automatic update"
473+
"that is going to show up next, and then delete the folder "
474+
"~/.config/jsoncomma. More details about the error in the console".format(
475+
old_path, new_path
476+
)
477+
)
478+
print("JSONComma: automatic fixup error:")
479+
traceback.print_exc()
480+
return False
481+
482+
return True

0 commit comments

Comments
 (0)