Unverified Commit 4833ad97 authored by Nick Cao's avatar Nick Cao Committed by GitHub
Browse files

sing-geosite: refactor without extra main.go (#393861)

parents 1b3fc55a 516b1793
Loading
Loading
Loading
Loading
+0 −52
Original line number Diff line number Diff line
func main() {
	outputFile, err := os.Create("geosite.db")
	if err != nil {
		panic(err)
	}
	defer outputFile.Close()
	vData, err := ioutil.ReadFile("@geosite_data@")
	if err != nil {
		panic(err)
	}
	domainMap, err := parse(vData)
	if err != nil {
		panic(err)
	}
	err = geosite.Write(outputFile, domainMap)
	if err != nil {
		panic(err)
	}

	ruleSetOutput := "rule-set"
	err = os.MkdirAll(ruleSetOutput, 0o755)
	if err != nil {
		panic(err)
	}
	for code, domains := range domainMap {
		var headlessRule option.DefaultHeadlessRule
		defaultRule := geosite.Compile(domains)
		headlessRule.Domain = defaultRule.Domain
		headlessRule.DomainSuffix = defaultRule.DomainSuffix
		headlessRule.DomainKeyword = defaultRule.DomainKeyword
		headlessRule.DomainRegex = defaultRule.DomainRegex
		var plainRuleSet option.PlainRuleSet
		plainRuleSet.Rules = []option.HeadlessRule{
			{
				Type:           C.RuleTypeDefault,
				DefaultOptions: headlessRule,
			},
		}
		srsPath, _ := filepath.Abs(filepath.Join(ruleSetOutput, "geosite-"+code+".srs"))
		os.Stderr.WriteString("write " + srsPath + "\n")
		outputRuleSet, err := os.Create(srsPath)
		if err != nil {
			panic(err)
		}
		err = srs.Write(outputRuleSet, plainRuleSet)
		if err != nil {
			outputRuleSet.Close()
			panic(err)
		}
		outputRuleSet.Close()
	}
}
+22 −17
Original line number Diff line number Diff line
@@ -2,31 +2,35 @@
  lib,
  buildGoModule,
  fetchFromGitHub,
  replaceVars,
  v2ray-domain-list-community,
}:

let
  patch = replaceVars ./main.go {
    geosite_data = "${v2ray-domain-list-community}/share/v2ray/geosite.dat";
  };
in
buildGoModule {
buildGoModule (finalAttrs: {
  pname = "sing-geosite";
  inherit (v2ray-domain-list-community) version;

  src = fetchFromGitHub {
    owner = "SagerNet";
    repo = "sing-geosite";
    rev = "bbd9f11bb9245463bf9d5614b74014fe5803b989";
    hash = "sha256-UQChYKgN5JZk+KZ2c5Ffh/rQi6/TVeFQkbH6mpLx4x8=";
    tag = "20250326132209";
    hash = "sha256-l9YjoxKxsEbWjhMuZC0NDsDjEQySdjdr34ix1NWNMlM=";
  };

  vendorHash = "sha256-C6idJDUp6AFe50tQ+4mmZsxuOKH8JSeC1p7XVRZ224E=";
  vendorHash = "sha256-35sCpEfelXcx8jQaOx7TO+X39NPuhStFmbLyLFooQcc=";

  patchPhase = ''
    sed -i -e '/func main()/,/^}/d' -e '/"io"/a "io/ioutil"' main.go
    cat ${patch} >> main.go
    sed -i main.go \
      -e '/"io"/a "io/ioutil"' \
      -e '/func main(/,/^}/d'

    substituteInPlace main.go --replace-fail \
      'vData, err := download(release)' \
      'vData, err := ioutil.ReadFile("${v2ray-domain-list-community}/share/v2ray/geosite.dat")'

    cat << EOF >> main.go
    func main() {
      generate(nil, "geosite.db", "geosite-cn.db", "rule-set", "rule-set-unstable")
    }
    EOF
  '';

  buildPhase = ''
@@ -42,10 +46,11 @@ buildGoModule {
    runHook postInstall
  '';

  meta = with lib; {
  meta = {
    description = "community managed domain list";
    homepage = "https://github.com/SagerNet/sing-geosite";
    license = licenses.gpl3Plus;
    maintainers = with maintainers; [ linsui ];
    license = lib.licenses.gpl3Plus;
    platforms = lib.platforms.all;
    maintainers = with lib.maintainers; [ linsui ];
  };
}
})