Skip to content

Commit

Permalink
config + dub
Browse files Browse the repository at this point in the history
  • Loading branch information
al1-ce committed Nov 24, 2022
1 parent b1119a7 commit 7c5f6f2
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 44 deletions.
39 changes: 32 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ Compilation of this repository requires [dlang](https://dlang.org).
1. Go to [releases](https://github.com/al1-ce/pkm/releases) and download binary.
2. Copy downloaded binary `./bin/pkm` to somewhere in your path, for example `~/.local/bin/`

<!-- ### 2.3 AUR -->
### 2.3 AUR

<!-- ### 2.4 dub -->
Not implemented yet

### 2.4 dub

1. Fetch package with `dub fetch pkm`
2. Run with `dub run pkm -b release -- [args...]`
3. Build and install into `/usr/bin` with `dub build -b release -c install`

## Commands

Expand All @@ -48,7 +54,25 @@ If you want to perform any of following command only on AUR then add `--aur` or
| stats | Print system statistics. | `yay -Ps`
| pkgbuild | Print PKGBUILD file | `yay -Gp [packages...]`

<!-- ## Config -->
## Config

pkm can be configured with config file located at `~/.config/pkm/conf.yaml` or `~/.pkm.yaml` one at `~` takes prority.

| Name | Type | Description | Default |
| :----| :--- | :---------- | :------ |
| yaypath | string | Custom path to yay binary. | Guessed with `which` |
| yaysearch | bool | Disable custom pkm search. | `false` |
| color | bool | Should search be printed in color. <br> Will not work if `yaysearch` is `true`. | `true` |
| auronly | bool | Should yay search only AUR. | `false` |

Example config:

```yaml
# conf.yaml
yaypath: ~/.local/bin/yay
yaysearch: yes
auronly: yes
```
## How to read search
All available `pkm` commands are calling `yay` with corresponding flags. This is true for search, but pkm also performs special operations to customise and improve yay's search.
Expand All @@ -61,16 +85,17 @@ package-name [a] [o] [i] version/installed-version package-size/votes install-
```

![](readme/screenshot_special.png)
![](readme/screenshot_special_bw.png)

Here's small table to assist you in reading it:

| Field | Meaning | Special notes |
| :- | :- | :- |
| package-name | Name of package. | |
| [a] | Is package orphaned. | Highlighted in red when true. "A" stands for abandoned. |
| [a] | Is package outdated. | Highlighted in red when true. |
| [a] | Is package installed. | Highlighted in green when true. |
| version | Version of package. | If installed version is different from current verison then field shows installed version hightlighed in light magenta. |
| [a] | Is package orphaned. | Highlighted in red when true. If color is disabled displayed as [ ]. "A" stands for abandoned. |
| [o] | Is package outdated. | Highlighted in red when true. If color is disabled displayed as [ ]. |
| [i] | Is package installed. | Highlighted in green when true. If color is disabled displayed as [ ]. |
| version | Version of package. | If installed version is different from current verison then field shows installed version hightlighed in light magenta. If color is disabled version diff shown with `@` at start. |
| package-size/votes | See notes. | If package from AUR: Package votes. <br> If package not from AUR: Package size.
| package-size/votes | See notes. | If package from AUR: Package popularity. <br> If package not from AUR: Installation size.
| [repo] | Repository of package. | Repository name is cropped to 3 symbols.
Expand Down
8 changes: 7 additions & 1 deletion dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@
"license": "MIT License",
"name": "pkm",
"targetName": "pkm",
"targetPath": "bin"
"targetPath": "bin/",
"configurations": [{
"name": "install",
"targetName": "pkm",
"targetPath": "bin",
"postBuildCommands": ["echo Copying binary to /usr/bin && sudo mv bin/pkm /usr/bin/pkm"]
}]
}
Binary file added readme/screenshot_special_bw.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 52 additions & 7 deletions src/pkm/app.d
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import std.stdio;
import std.getopt;
import std.array: popFront, join;
import std.process: execute, environment, executeShell, Config, spawnProcess, wait;
import std.process: execute, environment, executeShell, spawnProcess, wait;
import std.algorithm: canFind;
import std.file: readText, tempDir, remove, exists;
import std.path: buildNormalizedPath, absolutePath, expandTilde, baseName;

import pkm.search;
import pkm.config;

import sily.getopt;

Expand All @@ -27,7 +31,9 @@ import sily.getopt;
// stats | yay -Ps
// pkgbuild | yay -G term | yay -Gp term

private const string _version = "pkm v1.0.0";
private const string _version = "pkm v1.1.0";

string fixPath(string path) { return path.buildNormalizedPath.expandTilde.absolutePath; }

int main(string[] args) {
version(Windows) {
Expand Down Expand Up @@ -70,19 +76,58 @@ int main(string[] args) {
return 0;
}

string yay = "/usr/bin/yay";
string[] configPath = [
"~/.pkm.yaml".fixPath,
"~/.config/pkm/conf.yaml".fixPath,
];
Config conf = getConfig(configPath);

string yay = "";
bool yayDefined = false;
string cyay = conf.yaypath.fixPath;
if (cyay != "" && (
(cyay.exists && cyay.baseName == "yay") ||
(exists(cyay ~ "/yay")))) {
yayDefined = true;
yay = cyay;
} else if (cyay != "") {
writefln("Cannot find yay in \"%s\". \nAttempting to guess yay location.", conf.yaypath);
yay = "/usr/bin/yay";
}

if (!yayDefined) {
string tmpFile = tempDir ~ "/" ~ "pkm-yay-path.txt";
tmpFile = tmpFile.buildNormalizedPath.absolutePath;

auto processOut = File(tmpFile, "w+");
wait(spawnProcess(["which", "yay"], std.stdio.stdin, processOut));
processOut.close();
string _out = tmpFile.readText();
remove(tmpFile);

if (_out.canFind("which: no yay in")) {
writeln("Error: cannot find yay.");
return 1;
} else {
yay = _out;
}
}

string[] ops = args.dup;
ops.popFront(); // removes [0] command
ops.popFront(); // removes 'command'

if (optAur) {
if (optAur || conf.auronly) {
ops ~= ["--aur"];
}

switch (args[1]) {
case "search":
return search(ops);
if (conf.yaysearch) {
return wait(spawnProcess([yay, "-Ss"] ~ ops));
} else {
return search(ops, conf.color);
}
case "list":
return wait(spawnProcess([yay, "-Q"]));
case "info":
Expand All @@ -107,7 +152,7 @@ int main(string[] args) {
case "pkgbuild":
return wait(spawnProcess([yay, "-Gp"] ~ ops));
default:
writefln("Unknown command \"%s\".", args[1]);
return 1;
writefln("Unknown command \"%s\". Executing as is.", args[1]);
return wait(spawnProcess([yay] ~ ops));
}
}
77 changes: 77 additions & 0 deletions src/pkm/config.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
module pkm.config;

import std.conv: to;
import std.file: exists;
import std.stdio: writeln;

import dyaml;

Config getConfig(string[] paths) {
foreach (path; paths) {
if (path.exists) {
return __getConfig(path);
}
}
return Config();
}

Config __getConfig(string configPath) {
Node root;
try {
root = Loader.fromFile(configPath).load();
} catch (YAMLException e) {
return Config();
}

Config conf;

if (root.type != NodeType.mapping) return conf;

root.getKey!string(&conf.yaypath, "yaypath");
root.getKey!bool(&conf.yaysearch, "yaysearch");
root.getKey!bool(&conf.color, "color");
root.getKey!bool(&conf.auronly, "auronly");

return conf;
}

string configGetGlobal(string configPath, string field) {
Node root = Loader.fromFile(configPath).load();

if (root.type != NodeType.mapping) return "";
if (!root.containsKeyAs!string(field)) return "";

return root[field].as!string;
}

private bool containsKeyType(Node node, string key, NodeType type) {
if (node.containsKey(key)) {
if (node[key].type == type) {
return true;
}
}
return false;
}

private bool containsKeyAs(T)(Node node, string key) {
if (node.containsKey(key)) {
if (node[key].convertsTo!T) {
return true;
}
}
return false;
}

private void getKey(T)(Node node, T* variable, string field) {
if (node.containsKeyAs!T(field)) {
*variable = node[field].as!T;
}
}


struct Config {
string yaypath = "";
bool yaysearch = false;
bool color = true;
bool auronly = false;
}
Loading

0 comments on commit 7c5f6f2

Please sign in to comment.