Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update unversioned binaries #97

Closed
sigmaSd opened this issue Oct 1, 2023 · 12 comments · Fixed by #102
Closed

update unversioned binaries #97

sigmaSd opened this issue Oct 1, 2023 · 12 comments · Fixed by #102
Assignees
Labels
discussion Needs discussion to decide how to solve it

Comments

@sigmaSd
Copy link
Contributor

sigmaSd commented Oct 1, 2023

I think a cool feature would be to update unversioned binaries to their latest version

I have stuff like

       │ File: /home/mrcool/.deno/bin/deno_bindgen
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ #!/bin/sh
   2   │ # generated by deno install
   3   │ exec deno run --allow-all --quiet --no-config 'https://deno.land/x/deno_bindgen/cli.ts' "$@"
───────┴─────────────────────────────────────────────────────────────────────────────────────────────────

with this feature I expect an update form unversioned to x.x.x (whatver the latest is)

This will make suddenly most of my binaries compatible with nublar with no effort of my part as the user

@sigmaSd
Copy link
Contributor Author

sigmaSd commented Oct 1, 2023

I have this done locally but it requires changes to deno-udd (to handle unspecified version)

  • I can try to contribute this to upstream, but I'm not optimistic, udd didn't receive new features in a while now
  • Can we get away with not using udd, and just making our own custom regex replacer, that just search for deno.land urls, can scripts installed under deno/bin even have any other url ?

@sigmaSd
Copy link
Contributor Author

sigmaSd commented Oct 1, 2023

Here is how it looks btw
image

@hasundue
Copy link
Owner

hasundue commented Oct 1, 2023

It would be good if you could enable this feature with an option flag.

We might migrate to https://github.com/deaddeno/update or find a way to use https://github.com/hasundue/deno-molt ?

@sigmaSd
Copy link
Contributor Author

sigmaSd commented Oct 1, 2023

I feel like molt doesn't really fit, since the whole keypoint which is deno graph doesn't work here, unless maybe you expose some useful utilities

Does this really need a dependency can't we just regex search and replace deno urls ?

@hasundue
Copy link
Owner

hasundue commented Oct 1, 2023

I feel like molt doesn't really fit, since the whole keypoint which is deno graph doesn't work here,

True.
Just using regex is probably fine, since we do not really need variety of registries anymore after npm: specifier was introduced.

@hasundue hasundue added the discussion Needs discussion to decide how to solve it label Oct 1, 2023
@sigmaSd
Copy link
Contributor Author

sigmaSd commented Oct 22, 2023

I'm using this script for now

import homeDir from "https://deno.land/x/dir@1.5.1/home_dir/mod.ts";
import * as stdPath from "https://deno.land/std@0.204.0/path/mod.ts";
import * as infer from "npm:deno-infer@1.0.8";

function* getPrograms() {
  const homeDirPath = homeDir();
  if (!homeDirPath) throw "home dir not found";
  const programsDir = Deno.readDirSync(
    stdPath.join(homeDirPath, ".deno", "bin"),
  );
  for (const entry of programsDir) {
    if (!entry.isFile) continue;
    const path = stdPath.join(homeDirPath, ".deno", "bin", entry.name);
    //FIXME: windows have a different extension
    if (infer.getFromPath(path)?.extension() !== "sh") continue;
    yield {
      name: entry.name,
      path,
    };
  }
}

async function update(text: string) {
  let newText = text;
  for (
    const match of text.matchAll(
      /https:\/\/deno.land\/x\/([^@/]+)(@[^/]+)*\//g,
    )
  ) {
    let [_url, name, version] = match;
    // remove the @
    if (version) version = version.slice(1);
    const latestVersion: string | undefined = await fetch(
      `https://cdn.deno.land/${name}/meta/versions.json`,
    ).then((r) => r.json()).then((r) => r.latest);

    if (latestVersion === undefined) {
      console.error(
        "Could not determine package latest version, name: " + name,
      );
      return;
    }

    if (version !== latestVersion) {
      console.log(
        `${name} version: '${version}' remote-version: '${latestVersion}'`,
      );
      const oldUrl = version
        ? `https://deno.land/x/${name}@${version}/`
        : `https://deno.land/x/${name}/`;
      newText = newText.replace(
        oldUrl,
        `https://deno.land/x/${name}@${latestVersion}/`,
      );
    }
  }
  if (newText === text) return;
  return newText;
}

if (import.meta.main) {
  for (const program of getPrograms()) {
    const text = Deno.readTextFileSync(program.path);
    const newText = await update(text);
    if (newText !== undefined) {
      if (confirm(`Update ${program.name} ?`)) {
        Deno.writeTextFileSync(program.path, newText);
      }
    }
  }
}

@hasundue
Copy link
Owner

Thanks. Looks good, very clean.
Can I integrate your implementation into nublar, or should I ask you to create a PR with your authority?

@hasundue
Copy link
Owner

Ah maybe I can just use a routine in molt with a little work. Let me try.

@hasundue hasundue self-assigned this Oct 23, 2023
@hasundue hasundue added this to Deno Oct 23, 2023
@hasundue hasundue moved this to Todo in Deno Oct 23, 2023
@sigmaSd
Copy link
Contributor Author

sigmaSd commented Oct 23, 2023

It's cool there is nothing to copyright there, Iits better if nublar gets more features

@hasundue
Copy link
Owner

hasundue commented Nov 1, 2023

Now fixing molt for this
hasundue/molt#63

@hasundue hasundue linked a pull request Nov 2, 2023 that will close this issue
@github-project-automation github-project-automation bot moved this from Todo to Done in Deno Nov 2, 2023
@hasundue
Copy link
Owner

hasundue commented Nov 2, 2023

Done but not tested extensively. Bug reports would be very helpful.

@sigmaSd
Copy link
Contributor Author

sigmaSd commented Nov 2, 2023

thanks awesome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Needs discussion to decide how to solve it
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants