g-plane / zsh-yarn-autocompletions

Zsh plugin for Yarn autocompletions.
MIT License
578 stars 18 forks source link

zplug compatibility #1

Closed timdp closed 6 years ago

timdp commented 6 years ago

I'm by no means a zsh expert but I've been using zplug lately. I wanted to install your plugin using zplug and I ran into some issues:

  1. I was able to run the build manually by adding this to my .zshrc:

    zplug "g-plane/zsh-yarn-autocompletions", hook-build:"mkdir -p src && mv yarn-deps src/ && mv yarn-scripts src/ && cd src/yarn-deps && cargo build --release && cd ../yarn-scripts && cargo build --release && cd ../.. && mv src/yarn-deps/target/release/yarn-deps . && mv src/yarn-scripts/target/release/yarn-scripts ."

    In order to move the compiled yarn-deps and yarn-scripts to the plugin's root folder, the folders of the same name can't exist, so I'm moving those to src. It's kind of hacky so a better way would be nice.

  2. If I type yarn and hit tab, I get:

    _yarn_autocompletions_scripts:1: no such file or directory: /plugins/yarn-autocompletions/yarn-scripts
    _yarn_autocompletions_scripts:1: no such file or directory: /plugins/yarn-autocompletions/yarn-scripts
    _yarn_autocompletions_scripts:1: no such file or directory: /plugins/yarn-autocompletions/yarn-scripts

    Looking at the code, it's interpolating $ZSH_CUSTOM to the empty string, so I guess that's not supported by default. The plugin gets installed to ~/.zplug/repos/g-plane/zsh-yarn-autocompletions so $ZSH_CUSTOM/plugins/yarn-autocompletions, like the plugin code currently uses, doesn't seem like the generic approach.

Happy to have a stab at a PR but I wanted to check first. :slightly_smiling_face:

g-plane commented 6 years ago

I haven't used zplug but I will check it later. Also you can submit a PR.

g-plane commented 6 years ago

I am going to change the installation steps which may solve this problem.

It may be:

$ ./install.sh /path/to/you/like

So is the following example is OK? (This is a plan. Don't run it now.)

$ ./install.sh ~/.zplug/repos/g-plane
g-plane commented 6 years ago

You can try it in v1.0.0-0 (https://github.com/g-plane/zsh-yarn-autocompletions/releases/tag/v1.0.0-0)

timdp commented 6 years ago

I had a look at the code before running it. It might work, but I don't think you're using the API as intended.

The way I understand it, when I add zplug "g-plane/zsh-yarn-autocompletions" to .zshrc, that makes it clone the repo to ~/.zplug/repos/g-plane/zsh-yarn-autocompletions. For plugins written in pure zsh, that should be sufficient.

In this case, I assume you want to run rustc afterwards to get the binaries ready. zplug lets you specify a build hook in .zshrc, like I did in my original message. You could leverage that to compile the binaries.

I see you're still using $ZSH_CUSTOM in the code so it would most likely still fail. Couldn't you just get the plugin source directory as dirname $0 and put the binaries in a subdir there?

g-plane commented 6 years ago

You don't need to compile source code to binary file now. Just check the release page and download it.

According to your description, I guess plugins of zplug are located in ~/.zplug. If it is correct, you can run ./install.sh ~/.zplug/repos/g-plane.

And I am trying removing the $ZSH_CUSTOM.

g-plane commented 6 years ago

Using dirname $0 seems that it works well.

g-plane commented 6 years ago

Try https://github.com/g-plane/zsh-yarn-autocompletions/releases/tag/v1.0.0-1 ?

timdp commented 6 years ago

I appreciate the effort but I think I need to explain how zplug works, or at least as I understand it. The idea is that you can install plugins by simply adding a line of the format

zplug "user/repo"

to .zshrc. Then, when zplug initializes, it will notice that it doesn't have a local copy of that plugin and clone the repo. Additionally, it can automatically pull in new commits.

Thus, an install script that performs these steps for you isn't going to cut it as far as zplug is concerned. That is, it could sort of work but it doesn't seem like the right thing to do.

If a plugin install needs to run additional steps after git clone, it can do so with a post-build hook, like in my original message. There's probably also a way for plugins to specify those steps themselves so that users still only need to add a simple zplug statement; I didn't check.

Regardless, as a workaround, I considered adding the current install script as a build hook. Unfortunately, that's not going to work either because the install script insists on creating folders and copying files around, while the whole purpose of zplug is to do that for you. Additionally, as you mentioned, the build script doesn't produce binaries at all because you're expecting people to download the tarball from GitHub, which isn't how zplug works.

Now, I was able to make it work with:

zplug "g-plane/zsh-yarn-autocompletions", hook-build:"cargo build --release && cp target/release/yarn-autocompletions ./"

so that could already be part of the readme for zplug users. To make it slightly easier for them, you could add a build.sh that runs those commands. Or as mentioned above, there's probably zplug syntax for automating it. Or you could even do it just-in-time, right before the binary gets run for the first time; that way, you'd also avoid the hook.

If you don't want to introduce a dependency on the Rust toolchain (which makes sense), I think you can also still do that. If you also publish the binaries as part of a GitHub release, you can just curl those instead of compiling them locally.

Anyway, hope that helps!

g-plane commented 6 years ago

According to your description, is the following solution a good idea?

zplug "g-plane/zsh-yarn-autocompletions", hook-build:"sh zplug.sh"

And the zplug.sh would be:

wget # URL here

What do you think?

timdp commented 6 years ago

Yeah, that should work. I guess you'll have to determine the platform to decide which binary to download?

g-plane commented 6 years ago

How about just executing uname command?

g-plane commented 6 years ago

I have a question: If I use zplug to install a plugin, which shell script will be loaded when I open terminal?

timdp commented 6 years ago

I'm not sure what you mean. Did you check the zplug docs and check the code of existing plugins as a reference?

g-plane commented 6 years ago

For example, after we used zplug to install this plugin, will the yarn-autocompletions.plugin.zsh be loaded automatically?

timdp commented 6 years ago

It did do that for me, yeah. Not sure if it's a wildcard or an exact match.

g-plane commented 6 years ago

I have released https://github.com/g-plane/zsh-yarn-autocompletions/releases/tag/v1.1.0-beta.0 . You can try:

zplug "g-plane/zsh-yarn-autocompletions", hook-build:"sh zplug.sh"

Welcome sending any reponses.

danielbayerlein commented 6 years ago

zplug "g-plane/zsh-yarn-autocompletions", hook-build:"sh zplug.sh" does not work for me. I do not receive autocompletion.

timdp commented 6 years ago

It doesn't work because you're using sh instead of zsh. You can verify this by adding >/tmp/zplug.log 2>&1 to the command. Then you get this as the log:

zplug.sh: 3: [: Linux: unexpected operator
curl: (22) The requested URL returned error: 404 Not Found

gzip: stdin: unexpected end of file
tar: Child returned status 1
tar: Error is not recoverable: exiting now

If you add #!/usr/bin/zsh as the first line and make the script executable (chmod u+x zplug.sh), the build hook can just become ./zplug.sh. Additionally, if you add set -e before running any commands, the script will exit on errors, which is safer.

Also, what does the ./ in the tar command do?

timdp commented 6 years ago

Also, the comparison in the [] should use a single =. The == is invalid.

g-plane commented 6 years ago

Just extract the binary file only. However it seems that it will throw an error but the binary still can be extracted normally. It's confusing.

g-plane commented 6 years ago

@timdp In fact you can submit a PR, as I am not familiar with zplug.

timdp commented 6 years ago

My remarks were more about *NIX in general and zsh than zplug. :slightly_smiling_face:

jonleopard commented 6 years ago

@timdp Thanks a ton, man! I was just wondering if I could get this to work with zplug. The command

zplug "g-plane/zsh-yarn-autocompletions", hook-build:"cargo build --release && cp target/release/yarn-autocompletions ./"

worked like a charm :)

g-plane commented 6 years ago

@timdp So can you help me check the zplug.zsh file of the latest commit?

timdp commented 6 years ago

@g-plane I had to fix two things:

With those two changes, it worked.

g-plane commented 6 years ago

@timdp Is that OK now? I have updated it.

timdp commented 6 years ago

The script seems to work (the binary is there) but it's no longer completing when I hit tab. It's also broken with my cargo build hook, so I guess something else is broken?

g-plane commented 6 years ago

The CI result of last commit is passed. Can you show me the output of running cargo build manually?

timdp commented 6 years ago

Yeah, I'm not sure what could be wrong. It feels unrelated to the recent patches. In both cases (cargo and curl), I tested by removing the cloned repo, updating my .zshrc, and started a new shell, which completed the install process. Both installs worked and I verified that the binary was present. It's just that entering yarn and hitting tab just completes with file names rather than commands, as if the plugin isn't there at all. Since it worked before, I think there might be some other issue with my zsh installation.

g-plane commented 6 years ago

Have you installed other autocompletion plugins?

danielbayerlein commented 6 years ago

If I enter yarn run and press Tab, then I get the following error:

yarn run _fetch_yarn_autocompletions_result:1: no such file or directory: /usr/local/opt/zplug/repos/g-plane/zsh-yarn-autocompletions/yarn-autocompletions
_fetch_yarn_autocompletions_result:1: no such file or directory: /usr/local/opt/zplug/repos/g-plane/zsh-yarn-autocompletions/yarn-autocompletions
_fetch_yarn_autocompletions_result:1: no such file or directory: /usr/local/opt/zplug/repos/g-plane/zsh-yarn-autocompletions/yarn-autocompletions
yarn

$ ls /usr/local/opt/zplug/repos/g-plane/zsh-yarn-autocompletions

Cargo.lock                       install.sh                       yarn-autocompletions.example.yml
Cargo.toml                       package.json                     yarn-autocompletions.plugin.zsh
LICENSE                          screenshot.gif                   zplug.zsh
README.md                        src
g-plane commented 6 years ago

@danielbayerlein Have you tried the latest installation? The command may be zplug "g-plane/zsh-yarn-autocompletions", hook-build:"zplug.zsh".

danielbayerlein commented 6 years ago

@g-plane Yes. YARN_AUTO_COMP_PATH show the following path:

echo $YARN_AUTO_COMP_PATH
/usr/local/opt/zplug/repos/g-plane/zsh-yarn-autocompletions/yarn-autocompletions

But /usr/local/opt/zplug/repos/g-plane/zsh-yarn-autocompletions/yarn-autocompletions does not exist. I think the install.sh is not executed.

g-plane commented 6 years ago

@danielbayerlein install.sh is not for zplug user.

danielbayerlein commented 6 years ago

@g-plane But then the YARN_AUTO_COMP_PATH is wrong, right?

g-plane commented 6 years ago

@danielbayerlein Is the binary file in the plugin directory?

danielbayerlein commented 6 years ago

@g-plane See the directory output in my comment above.

g-plane commented 6 years ago

@danielbayerlein According to your comment, there is no binary files. So it's impossible to execute autocompletion.

danielbayerlein commented 6 years ago

@g-plane You're right. Apparently the hook-build does not work. If I execute ./zplug.zsh manually, then the yarn autocompletion works.

{"pid":58671,"shlvl":1,"level":"ERROR","dir":"/usr/local/opt/zplug/repos/g-plane/zsh-yarn-autocompletions","message":"(eval):1: command not found: zplug.zsh","trace":["__zplug::log::capture::error:8","__zplug::job::hook::service:25","__zplug::job::hook::build:4","__zplug::job::handle::hook:32","__zplug::job::handle::wait:41","__install__:73","__zplug::core::core::run_interfaces:14","zplug:12","zsh:4"],"date":"2018-03-30T11:01:18+0200"}
g-plane commented 6 years ago

@timdp What is your installation command?

danielbayerlein commented 6 years ago

šŸŽ‰ The solution for zsh-yarn-autocompletions and zplug šŸŽ‰

zplug "g-plane/zsh-yarn-autocompletions", hook-build:"./zplug.zsh", defer:2

@g-plane Are you interested on a pull request for the documentation?

g-plane commented 6 years ago

@danielbayerlein Please do.