diff --git a/examples/.envrc b/examples/.envrc new file mode 100644 index 00000000..1281ada9 --- /dev/null +++ b/examples/.envrc @@ -0,0 +1,10 @@ +source_up + +files=(../../flake.nix flake-module.nix package.json) +if type nix_direnv_watch_file &>/dev/null; then + nix_direnv_watch_file "${files[@]}" +else + watch_file "${files[@]}" +fi + +use flake .#builtins-data --builders '' diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 00000000..809ffcf3 --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1,2 @@ +data.json +node_modules \ No newline at end of file diff --git a/examples/flake-module.nix b/examples/flake-module.nix new file mode 100644 index 00000000..338c531e --- /dev/null +++ b/examples/flake-module.nix @@ -0,0 +1,17 @@ +{ inputs, ... }: { + perSystem = { self', inputs', pkgs, lib, ... }: + { + packages = { + nixpkgs-examples = pkgs.stdenv.mkDerivation { + name = "nixpkgs-examples"; + src = ./.; + nativeBuildInputs = [ pkgs.python3 ]; + buildPhase = '' + cp -f ${self'.packages.data-json} ./data.json + python noogle_get_examples.py data.json ${inputs.nixpkgs-master} + mv output.json $out + ''; + }; + }; + }; +} diff --git a/examples/noogle_get_examples.py b/examples/noogle_get_examples.py new file mode 100644 index 00000000..2d35051c --- /dev/null +++ b/examples/noogle_get_examples.py @@ -0,0 +1,72 @@ +#!/usr/bin/env nix-shell +#!nix-shell -p python3 + + + +import json +import os +import sys +# Get file and folder path from command line arguments +json_file_path = sys.argv[1] +folder_path = sys.argv[2] + +# Load JSON file +with open(json_file_path, 'r') as f: + data = json.load(f) + + +search_strings = set() +for entry in data: + # Walk through folder structure and search for matches + search_strings.add(entry['meta']['path'][-1]) + count = 0 +search_strings = ["callPackage", "or", "toInt"] + +search_strings = dict.fromkeys(search_strings, {"count": 0, "matches": []}) + +finished_search_strings = dict() + +for root, dirs, files in os.walk(folder_path): + for file in files: + path = os.path.join(root, file) + if file.endswith(".nix") and not os.path.islink(path) and os.path.isfile(path): + with open(path, 'r') as f: + lines = f.readlines() + for line in lines: + for string in search_strings: + if f" {string} " in line: + matches = search_strings[string]["matches"].copy() + linenumber = lines.index(line) + 1 + match = {"line": linenumber, "column": None, "example": lines[linenumber-3:linenumber+3], "file": os.path.join(root, file)} + matches.append(match) + string_dict = search_strings[string].copy() + string_dict.update( + {"count": search_strings[string]["count"]+1, + "matches": matches}) + search_strings.update({string: string_dict}) + + left_search_strings = {} + for string in search_strings: + if search_strings[string]["count"] < 3: + left_search_strings.update({string: search_strings[string]}) + else: + finished_search_strings.update({string: search_strings[string]}) + search_strings = left_search_strings + print(f"{len(search_strings)} left") +finished_search_strings.update(search_strings) + + +output = {} +for entry in data: + # Walk through folder structure and search for matches + if entry['meta']['path'][-1] in finished_search_strings: + output.update({"meta": {"path": entry["meta"]["path"], "examples": finished_search_strings[entry['meta']['path'][-1]]}}) + +with open("output.json", "w") as outfile: + outfile.write(json.dumps(output, indent=4)) + + + + + + diff --git a/flake.nix b/flake.nix index 29007750..15c96852 100644 --- a/flake.nix +++ b/flake.nix @@ -5,7 +5,7 @@ nixpkgs-master.url = "nixpkgs/master"; nix-master.url = "github:NixOS/nix/?ref=master"; - # A custom nix version, to introspect lambda values. + # A custom nix version, to introspect lambda values. nix.url = "github:hsjobeki/nix/?ref=feat/positions"; pre-commit-hooks = { @@ -33,6 +33,7 @@ ./preCommit.nix ./website/flake-module.nix ./salt/flake-module.nix + ./examples/flake-module.nix ./pasta/flake-module.nix ./pesto/flake-module.nix # Deprecated. Will be removed.