Skip to content

Commit

Permalink
fix: version compare
Browse files Browse the repository at this point in the history
  • Loading branch information
qiin2333 committed Jan 14, 2025
1 parent f053750 commit 70116ef
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 162 deletions.
279 changes: 144 additions & 135 deletions src_assets/common/assets/web/index.html
Original file line number Diff line number Diff line change
@@ -1,156 +1,165 @@
<!DOCTYPE html>
<html lang="en" data-bs-theme="auto">
<head>
<%- header %>
</head>

<head>
<%- header %>
</head>

<body id="app" v-cloak>
<Navbar></Navbar>
<div id="content" class="container">
<h1 class="my-4">{{ $t('index.welcome') }}</h1>
<p>{{ $t('index.description') }}</p>
<div class="alert alert-danger" v-if="fancyLogs.find(x => x.level === 'Fatal')">
<div style="line-height: 32px;">
<i class="fas fa-circle-exclamation" style="font-size: 32px;margin-right: 0.25em;"></i>
<p v-html="$t('index.startup_errors')"></p>
<br>
</div>
<ul>
<li v-for="v in fancyLogs.filter(x => x.level === 'Fatal')">{{v.value}}</li>
</ul>
<a class="btn btn-danger" href="/troubleshooting/#logs">View Logs</a>
</div>
<!-- Version -->
<div class="card p-2 my-4">
<div class="card-body" v-if="version">
<h2>Version {{version.version}}</h2>
<br>
<div v-if="loading">
{{ $t('index.loading_latest') }}
</div>
<div class="alert alert-success" v-if="buildVersionIsDirty">
{{ $t('index.version_dirty') }} 🌇
<body id="app" v-cloak>
<Navbar></Navbar>
<div id="content" class="container">
<h1 class="my-4">{{ $t('index.welcome') }}</h1>
<p>{{ $t('index.description') }}</p>
<div class="alert alert-danger" v-if="fancyLogs.find(x => x.level === 'Fatal')">
<div style="line-height: 32px">
<i class="fas fa-circle-exclamation" style="font-size: 32px; margin-right: 0.25em"></i>
<p v-html="$t('index.startup_errors')"></p>
<br />
</div>
<div class="alert alert-info" v-if="installedVersionNotStable">
{{ $t('index.installed_version_not_stable') }}
</div>
<div v-else-if="(!preReleaseBuildAvailable || !notifyPreReleases) && !stableBuildAvailable && !buildVersionIsDirty">
<div class="alert alert-success">
{{ $t('index.version_latest') }}
<ul>
<li v-for="v in fancyLogs.filter(x => x.level === 'Fatal')">{{v.value}}</li>
</ul>
<a class="btn btn-danger" href="/troubleshooting/#logs">View Logs</a>
</div>
<!-- Version -->
<div class="card p-2 my-4">
<div class="card-body" v-if="version">
<h2>Version {{version.version}}</h2>
<br />
<div v-if="loading">{{ $t('index.loading_latest') }}</div>
<div class="alert alert-success" v-if="buildVersionIsDirty">{{ $t('index.version_dirty') }} 🌇</div>
<div class="alert alert-info" v-if="installedVersionNotStable">
{{ $t('index.installed_version_not_stable') }}
</div>
</div>
<div v-if="notifyPreReleases && preReleaseBuildAvailable">
<div class="alert alert-warning">
<div class="d-flex justify-content-between">
<div class="my-2">有新的 <b>基地版</b> sunshine可以更新!</div>
<a class="btn btn-success m-1" :href="preReleaseVersion.release.html_url"
target="_blank">{{ $t('index.download')
}}</a>
<div
v-else-if="(!preReleaseBuildAvailable || !notifyPreReleases) && !stableBuildAvailable && !buildVersionIsDirty"
>
<div class="alert alert-success">{{ $t('index.version_latest') }}</div>
</div>
<div v-if="notifyPreReleases && preReleaseBuildAvailable">
<div class="alert alert-warning">
<div class="d-flex justify-content-between">
<div class="my-2">有新的 <b>基地版</b> sunshine可以更新!</div>
<a class="btn btn-success m-1" :href="preReleaseVersion.release.html_url" target="_blank"
>{{ $t('index.download') }}</a
>
</div>
<pre><b>{{preReleaseVersion.release.name}}</b></pre>
<pre>{{preReleaseVersion.release.body}}</pre>
</div>
<pre><b>{{preReleaseVersion.release.name}}</b></pre>
<pre>{{preReleaseVersion.release.body}}</pre>
</div>
</div>
<div v-if="stableBuildAvailable">
<div class="alert alert-warning">
<div class="d-flex justify-content-between">
<div class="my-2">{{ $t('index.new_stable') }}</div>
<a class="btn btn-success m-1" :href="githubVersion.release.html_url" target="_blank">{{ $t('index.download') }}</a>
<div v-if="stableBuildAvailable">
<div class="alert alert-warning">
<div class="d-flex justify-content-between">
<div class="my-2">{{ $t('index.new_stable') }}</div>
<a class="btn btn-success m-1" :href="githubVersion.release.html_url" target="_blank"
>{{ $t('index.download') }}</a
>
</div>
<h3>{{githubVersion.release.name}}</h3>
<pre v-html="githubVersion.release.body"></pre>
</div>
<h3>{{githubVersion.release.name}}</h3>
<pre v-html="githubVersion.release.body"></pre>
</div>
</div>
</div>
<!-- Resources -->
<div class="my-4">
<Resource-Card></Resource-Card>
</div>
</div>
<!-- Resources -->
<div class="my-4">
<Resource-Card></Resource-Card>
</div>
</div>
</body>
</body>

<script type="module">
import { createApp } from 'vue'
import { initApp } from './init'
import Navbar from './Navbar.vue'
import ResourceCard from './ResourceCard.vue'
import SunshineVersion from './sunshine_version'
<script type="module">
import { createApp } from 'vue'
import { initApp } from './init'
import Navbar from './Navbar.vue'
import ResourceCard from './ResourceCard.vue'
import SunshineVersion from './sunshine_version'

console.log("Hello, Sunshine!")
let app = createApp({
components: {
Navbar,
ResourceCard
},
data() {
return {
version: null,
githubVersion: null,
notifyPreReleases: false,
preReleaseVersion: null,
loading: true,
logs: null,
}
},
async created() {
try {
let config = await fetch("/api/config").then((r) => r.json());
this.notifyPreReleases = config.notify_pre_releases;
this.version = new SunshineVersion(null, config.version);
console.log("Version: ", this.version.version)
this.githubVersion = new SunshineVersion(await fetch("https://api.github.com/repos/qiin2333/Sunshine/releases/latest").then((r) => r.json()), null);
console.log("GitHub Version: ", this.githubVersion.version)
this.preReleaseVersion = new SunshineVersion((await fetch("https://api.github.com/repos/qiin2333/Sunshine/releases").then((r) => r.json())).find(release => release.prerelease), null);
console.log("Pre-Release Version: ", this.preReleaseVersion.version)
} catch (e) {
console.error(e);
}
try {
this.logs = (await fetch("/api/logs").then(r => r.text()))
} catch (e) {
console.error(e);
}
if (this.version) document.title += ` Ver ${this.version.version}`;
this.loading = false;
},
computed: {
installedVersionNotStable() {
if (!this.githubVersion || !this.version) {
return false;
}
return this.version.isGreater(this.githubVersion);
console.log('Hello, Sunshine!')
let app = createApp({
components: {
Navbar,
ResourceCard,
},
stableBuildAvailable() {
if (!this.githubVersion || !this.version) {
return false;
data() {
return {
version: null,
githubVersion: null,
notifyPreReleases: false,
preReleaseVersion: null,
loading: true,
logs: null,
}
return this.githubVersion.isGreater(this.version);
},
preReleaseBuildAvailable() {
if (!this.preReleaseVersion || !this.githubVersion || !this.version) {
return false;
async created() {
try {
let config = await fetch('/api/config').then((r) => r.json())
this.notifyPreReleases = config.notify_pre_releases
this.version = new SunshineVersion(null, config.version)
console.log('Version: ', this.version.version)
this.githubVersion = new SunshineVersion(
await fetch('https://api.github.com/repos/qiin2333/Sunshine/releases/latest').then((r) => r.json()),
null
)
console.log('GitHub Version: ', this.githubVersion.version)
this.preReleaseVersion = new SunshineVersion(
(await fetch('https://api.github.com/repos/qiin2333/Sunshine/releases').then((r) => r.json())).find(
(release) => release.prerelease
),
null
)
console.log('Pre-Release Version: ', this.preReleaseVersion.version)
} catch (e) {
console.error(e)
}
try {
this.logs = await fetch('/api/logs').then((r) => r.text())
} catch (e) {
console.error(e)
}
return this.preReleaseVersion.isGreater(this.version);
if (this.version) document.title += ` Ver ${this.version.version}`
this.loading = false
},
buildVersionIsDirty() {
return this.version.version?.split(".").length === 5 &&
this.version.version.indexOf("dirty") !== -1
computed: {
installedVersionNotStable() {
if (!this.githubVersion || !this.version) {
return false
}
return this.version.isGreater(this.githubVersion)
},
stableBuildAvailable() {
if (!this.githubVersion || !this.version) {
return false
}
return this.githubVersion.isGreater(this.version)
},
preReleaseBuildAvailable() {
if (!this.preReleaseVersion || !this.githubVersion || !this.version) {
return false
}
return this.preReleaseVersion.isGreater(this.version)
},
buildVersionIsDirty() {
return this.version.version?.split('.').length === 5 && this.version.version.indexOf('dirty') !== -1
},
/** Parse the text errors, calculating the text, the timestamp and the level */
fancyLogs() {
if (!this.logs) return []
let regex = /(\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}]):\s/g
let rawLogLines = this.logs.split(regex).splice(1)
let logLines = []
for (let i = 0; i < rawLogLines.length; i += 2) {
logLines.push({
timestamp: rawLogLines[i],
level: rawLogLines[i + 1].split(':')[0],
value: rawLogLines[i + 1],
})
}
return logLines
},
},
/** Parse the text errors, calculating the text, the timestamp and the level */
fancyLogs() {
if (!this.logs) return [];
let regex = /(\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}]):\s/g;
let rawLogLines = (this.logs.split(regex)).splice(1);
let logLines = []
for (let i = 0; i < rawLogLines.length; i += 2) {
logLines.push({timestamp: rawLogLines[i], level: rawLogLines[i + 1].split(":")[0], value: rawLogLines[i + 1]});
}
return logLines;
}
}
});
})

initApp(app);
</script>
initApp(app)
</script>
</html>
59 changes: 32 additions & 27 deletions src_assets/common/assets/web/sunshine_version.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,60 @@
class SunshineVersion {
constructor(release = null, version = null) {
if (release) {
this.release = release;
this.version = release.tag_name;
this.versionName = release.name;
this.versionTag = release.tag_tag;
this.release = release
this.version = release.tag_name
this.versionName = release.name
this.versionTag = release.tag_tag
} else if (version) {
this.release = null;
this.version = version;
this.versionName = null;
this.versionTag = null;
this.release = null
this.version = version
this.versionName = null
this.versionTag = null
} else {
throw new Error('Either release or version must be provided');
throw new Error('Either release or version must be provided')
}
this.versionParts = this.parseVersion(this.version);
this.versionMajor = this.versionParts ? this.versionParts[0] : null;
this.versionMinor = this.versionParts ? this.versionParts[1] : null;
this.versionPatch = this.versionParts ? this.versionParts[2] : null;
this.versionParts = this.parseVersion(this.version)
this.versionMajor = this.versionParts ? this.versionParts[0] : null
this.versionMinor = this.versionParts ? this.versionParts[1] : null
this.versionPatch = this.versionParts ? this.versionParts[2] : null
}

parseVersion(version) {
if (!version) {
return null;
return null
}
let v = version;
if (v.indexOf("v") === 0) {
v = v.substring(1);
let v = version
if (v.indexOf('v') === 0) {
v = v.substring(1)
}
return v.split('.').map(Number);
return v.split('.').map(Number)
}

isGreater(otherVersion) {
let otherVersionParts;
let otherVersionParts
if (otherVersion instanceof SunshineVersion) {
otherVersionParts = otherVersion.versionParts;
otherVersionParts = otherVersion.versionParts
} else if (typeof otherVersion === 'string') {
otherVersionParts = this.parseVersion(otherVersion);
otherVersionParts = this.parseVersion(otherVersion)
} else {
throw new Error('Invalid argument: otherVersion must be a SunshineVersion object or a version string');
throw new Error('Invalid argument: otherVersion must be a SunshineVersion object or a version string')
}

if (!this.versionParts || !otherVersionParts) {
return false;
return false
}
for (let i = 0; i < Math.min(3, this.versionParts.length, otherVersionParts.length); i++) {
if (this.versionParts[i] > otherVersionParts[i]) {
return true;
const v1 = this.versionParts[i]
const v2 = otherVersionParts[i]
if (v1 > v2) {
return true
}
if (v1 < v2) {
return false
}
}
return false;
return false
}
}

export default SunshineVersion;
export default SunshineVersion

0 comments on commit 70116ef

Please sign in to comment.