]> git.proxmox.com Git - pve-docs.git/blobdiff - scan-adoc-refs
scan-adoc-refs: auto generate dependencies for Makefile
[pve-docs.git] / scan-adoc-refs
index 551791f2fa7fb47f02a7979b1a02706fc6afb915..bc893249c7b0c8da6c10a25a40493e0c29ed8546 100755 (executable)
@@ -2,11 +2,18 @@
 
 use strict;
 use warnings;
+use Getopt::Long;
 use IO::File;
 use JSON;
 
 use Data::Dumper;
 
+my $generate_depends;
+
+GetOptions("depends=s" => \$generate_depends) or
+    die("Error in command line arguments\n");
+
+
 my $environments = {
     default => 1,
     wiki => 1,
@@ -14,86 +21,7 @@ my $environments = {
     pvelogo => 0, # ignore
 };
 
-my $fileinfo = {
-    outfile => {
-       default => {
-           "pve-admin-guide.adoc" => "pve-admin-guide.html",
-           "datacenter.cfg.adoc" => "datacenter.cfg.5.html",
-           "ha-manager.adoc" => "chapter-ha-manager.html",
-           "pct.adoc" => "chapter-pct.html",
-           "pve-bibliography.adoc" => "chapter-pve-bibliography.html",
-           "pve-firewall.adoc" => "chapter-pve-firewall.html",
-           "pve-installation.adoc" => "chapter-pve-installation.html",
-           "pvecm.adoc" => "chapter-pvecm.html",
-           "pvesm.adoc" => "chapter-pvesm.html",
-           "pveum.adoc" => "chapter-pveum.html",
-           "qm.adoc" => "chapter-qm.html",
-           "sysadmin.adoc" => "chapter-sysadmin.html",
-           "vzdump.adoc" => "chapter-vzdump.html",
-           "pmxcfs.adoc" => "chapter-pmxcfs.html",
-           "pve-faq.adoc" => "chapter-pve-faq.html",
-       },
-       manvolnum => {
-           "ha-manager.adoc" => "ha-manager.1",
-           "pct.adoc" => "pct.1",
-           "pveam.adoc" => "pveam.1",
-           "pveceph.adoc" => "pveceph.1",
-           "pvecm.adoc" => "pvecm.1",
-           "pveperf.adoc" => "pveperf.1",
-           "pvesm.adoc" => "pvesm.1",
-           "pvesubscription.adoc" => "pvesubscription.1",
-           "pveum.adoc" => "pveum.1",
-           "qm.adoc" => "qm.1",
-           "qmrestore.adoc" => "qmrestore.1",
-           "vzdump.adoc" => "vzdump.1",
-           "datacenter.cfg.adoc" => "datacenter.cfg.5",
-           "pct.conf.adoc" => "pct.conf.5",
-           "qm.conf.adoc" => "qm.conf.5",
-           "pmxcfs.adoc" => "pmxcfs.8",
-           "pvedaemon.adoc" => "pvedaemon.8",
-           "pve-firewall.adoc" => "pve-firewall.8",
-           "pve-ha-crm.adoc" => "pve-ha-crm.8",
-           "pve-ha-lrm.adoc" => "pve-ha-lrm.8",
-           "pveproxy.adoc" => "pveproxy.8",
-           "pvestatd.adoc" => "pvestatd.8",
-           "spiceproxy.adoc" => "spiceproxy.8",
-       },
-       wiki => {
-           "pve-usbstick.adoc" => "pve-usbstick-plain.html",
-           "getting-help.adoc" => "getting-help-plain.html",
-           "pve-system-requirements.adoc" => "pve-system-requirements-plain.html",
-           "pve-network.adoc" => "pve-network-plain.html",
-           "pve-package-repos.adoc" => "pve-package-repos-plain.html",
-           "system-software-updates.adoc" => "system-software-updates-plain.html",
-           "pve-disk-health-monitoring.adoc" => "pve-disk-health-monitoring-plain.html",
-           "local-lvm.adoc" => "local-lvm-plain.html",
-           "local-zfs.adoc" => "local-zfs-plain.html",
-           "system-timesync.adoc" => "system-timesync-plain.html",
-           "pve-installation.adoc" => "pve-installation-plain.html",
-           "sysadmin.adoc" => "sysadmin-plain.html",
-           "pvecm.adoc" => "pvecm-plain.html",
-           "pmxcfs.adoc" => "pmxcfs-plain.html",
-           "pvesm.adoc" => "pvesm-plain.html",
-           "qm.adoc" => "qm-plain.html",
-           "pve-firewall.adoc" => "pve-firewall-plain.html",
-           "pveum.adoc" => "pveum-plain.html",
-           "pct.adoc" => "pct-plain.html",
-           "ha-manager.adoc" => "ha-manager-plain.html",
-           "vzdump.adoc" => "vzdump-plain.html",
-           "pve-faq.adoc" => "pve-faq-plain.html",
-           "pve-bibliography.adoc" => "pve-bibliography-plain.html",
-           "pve-storage-dir.adoc" => "pve-storage-dir-plain.html",
-           "pve-storage-glusterfs.adoc" => "pve-storage-glusterfs-plain.html",
-           "pve-storage-iscsi.adoc" => "pve-storage-iscsi-plain.html",
-           "pve-storage-iscsidirect.adoc" => "pve-storage-iscsidirect-plain.html",
-           "pve-storage-lvm.adoc" => "pve-storage-lvm-plain.html",
-           "pve-storage-lvmthin.adoc" => "pve-storage-lvmthin-plain.html",
-           "pve-storage-nfs.adoc" => "pve-storage-nfs-plain.html",
-           "pve-storage-rbd.adoc" => "pve-storage-rbd-plain.html",
-           "pve-storage-zfspool.adoc" => "pve-storage-zfspool-plain.html",
-       },
-    },
-};
+my $fileinfo = {};
 
 my $start_env = [];
 foreach my $e (keys %$environments) {
@@ -148,8 +76,6 @@ sub pop_environment {
 sub register_include {
     my ($filename, $include_filename, $env_list) = @_;
 
-    return if $include_filename !~ m/\.adoc$/; # skip attributes.txt
-
     foreach my $e (@$env_list) {
        $fileinfo->{include}->{$e}->{$filename}->{$include_filename} = 1;
     }
@@ -186,11 +112,14 @@ sub register_title {
            die "unable to change title (no doctype)"
                if !defined($fileinfo->{doctype}->{$env}->{$filename});
        }
+    } elsif (!defined($doctype)) {
+       # change title via :title: attribute
+       $fileinfo->{titles}->{$env}->{$filename} = $title;
+    }
 
-       if (defined($doctype) && ($env eq 'manvolnum') && ($doctype == 0)) {
-           if ($title =~ m/.*\(([1-8])\)\s*$/) {
-               $fileinfo->{mansection}->{$env}->{$filename} = $1;
-           }
+    if (defined($doctype) && ($env eq 'manvolnum') && ($doctype == 0)) {
+       if ($title =~ m/.*\(([1-8])\)\s*$/) {
+           $fileinfo->{mansection}->{$env}->{$filename} = $1;
        }
     }
 
@@ -333,6 +262,45 @@ sub resolve_link_target {
     return $filename;
 }
 
+
+# try to generate output file mapping
+foreach my $e (@$start_env) {
+    my $toplevel_hash = $fileinfo->{toplevel}->{$e};
+    foreach my $fn (sort keys %$toplevel_hash) {
+       my $mansection = $fileinfo->{mansection}->{manvolnum}->{$fn};
+       if ($e eq 'wiki') {
+           my $realfn = $fn;
+           $realfn =~ s/\.adoc$//;
+           if (defined($mansection) && ($mansection == 5)) {
+               $realfn .= ".$mansection";
+           }
+           $realfn = "$realfn-plain.html";
+           $fileinfo->{outfile}->{$e}->{$fn} = $realfn;
+       } elsif ($e eq 'manvolnum') {
+           my $realfn = $fn;
+           $realfn =~ s/\.adoc$//;
+           die "toplevel file '$fn' is not marked as manual page!" if !$mansection;
+           $realfn .= ".$mansection";
+           $fileinfo->{outfile}->{$e}->{$fn} = $realfn;
+       } elsif ($e eq 'default') {
+           my $realfn = $fn;
+           $realfn =~ s/\.adoc$//;
+           if (defined($mansection) && ($mansection == 5)) {
+               $realfn .= ".$mansection";
+               $realfn = "$realfn.html";
+           } else {
+               if (($fn ne 'pve-admin-guide.adoc') &&
+                   $fileinfo->{doctype}->{$e}->{$fn} == 0) {
+                   $realfn = "chapter-$realfn.html";
+               } else {
+                   $realfn = "$realfn.html";
+               }
+           }
+           $fileinfo->{outfile}->{$e}->{$fn} = $realfn;
+       }
+    }
+}
+
 # now resolve blockids
 foreach my $e (@$start_env) {
     my $blockid_hash = $fileinfo->{blockid}->{$e};
@@ -343,6 +311,11 @@ foreach my $e (@$start_env) {
            $title =~ s/\s/_/g;
            die "found not title for '$fn' in env '$e'" if !$title;
            $fileinfo->{blockid_target}->{$e}->{$blockid} = "link:/wiki/$title#$blockid";
+
+           # we do not produce wiki pages for all content
+           #my $realfn = $fileinfo->{outfile}->{$e}->{$fn};
+           #warn "no output file mapping for '$fn' ($e)\n" if !$realfn;
+
        } elsif ($e eq 'default') {
            my $realfn = $fileinfo->{outfile}->{$e}->{$fn} ||
                die "no output file mapping for '$fn'\n";
@@ -356,5 +329,102 @@ foreach my $e (@$start_env) {
     }
 }
 
+if ($generate_depends) {
+
+    my $tmpfilename = "${generate_depends}.tmp";
+    my $outfh = IO::File->new($tmpfilename, "w") ||
+       die "unable to open temporary file '$tmpfilename'\n";
+
+    my $depends = {};
+    foreach my $e (@$start_env) {
+       my $env_data = $fileinfo->{include}->{$e};
+       foreach my $fn (keys %$env_data) {
+           foreach my $dep (keys %{$env_data->{$fn}}) {
+               $depends->{$fn}->{$dep} = 1;
+           }
+       }
+    }
+
+    my $res = '';
+
+    my $depend_varname_hash = {};
+
+    foreach my $fn (sort keys %$depends) {
+       my $basename = uc($fn);
+       $basename =~s/\.adoc$//i;
+       $basename =~s/[^A-Za-z0-9]/_/g;
+
+       my $var1text = "${basename}_ADOCSOURCES =";
+       my $var2text = "${basename}_ADOCDEPENDS = \$\{${basename}_ADOCSOURCES\}";
+       $depend_varname_hash->{$fn} = "${basename}_ADOCDEPENDS";
+
+       foreach my $dep (sort keys %{$depends->{$fn}}) {
+           if ($dep =~ m/-(opts|synopsis).adoc$/) {
+               $var2text .= " \\\n\t$dep";
+           } else {
+               $var1text .= " \\\n\t$dep";
+           }
+       }
+       $res .= "$var1text\n\n";
+       $res .= "$var2text\n\n";
+    }
+
+    my $man_sources_hash = {};
+    foreach my $e (@$start_env) {
+       my $filelist = $fileinfo->{outfile}->{$e};
+       foreach my $sourcefile (sort keys %$filelist) {
+           my $varname = $depend_varname_hash->{$sourcefile};
+           my $target = $filelist->{$sourcefile};
+           $res .= "$target: \$\{$varname\}\n\n";
+
+           if ($e eq 'manvolnum') {
+               $man_sources_hash->{$sourcefile} = 1;
+
+               $res .= "$target.html: \$\{$varname\}\n\n";
+               $res .= "$target-plain.html: \$\{$varname\}\n\n";
+           }
+       }
+    }
+
+    my $varname = "MANUAL_SOURCES";
+    $res .= "$varname =";
+    foreach my $sourcefile (sort keys %$man_sources_hash) {
+       $res .= " \\\n\t$sourcefile";
+    }
+    $res .= "\n\n";
+
+    $varname = "CHAPTER_LIST";
+    $res .= "$varname =";
+    my $filelist = $fileinfo->{outfile}->{default};
+    foreach my $sourcefile (sort keys %$filelist) {
+       my $target = $filelist->{$sourcefile};
+       $res .= " \\\n\t$target";
+    }
+    $res .= "\n\n";
+
+    $varname = "MANUAL_PAGES";
+    $res .= "$varname =";
+    $filelist = $fileinfo->{outfile}->{manvolnum};
+    foreach my $manpage (sort keys %$filelist) {
+       my $target = $filelist->{$manpage};
+       $res .= " \\\n\t$target";
+    }
+    $res .= "\n\n";
+
+    $varname = "WIKI_IMPORTS";
+    $res .= "$varname =";
+    $filelist = $fileinfo->{outfile}->{wiki};
+    foreach my $sourcefile (sort keys %$filelist) {
+       my $target = $filelist->{$sourcefile};
+       $res .= " \\\n\t$target";
+    }
+    $res .= "\n\n";
+
+    print $outfh $res;
+    close($outfh);
+
+    rename($tmpfilename, $generate_depends) ||
+       die "rename failed - $!";
+}
 
 print to_json($fileinfo, { pretty => 1,  canonical => 1 } );