Skip to content

Commit

Permalink
feat: fetch latest tag from github api
Browse files Browse the repository at this point in the history
  • Loading branch information
Curve committed Oct 28, 2023
1 parent 967f7b0 commit c68a915
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 33 deletions.
26 changes: 26 additions & 0 deletions docs/getting-started/basic-app.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.21)
project(your_awesome_app LANGUAGES CXX VERSION 1.0)

# --------------------------------------------------------------------------------------------------------
# Create executable
# --------------------------------------------------------------------------------------------------------

add_executable(${PROJECT_NAME} "main.cpp")
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 20 CXX_EXTENSIONS OFF CXX_STANDARD_REQUIRED ON)

# --------------------------------------------------------------------------------------------------------
# Link libraries
# --------------------------------------------------------------------------------------------------------

include(FetchContent)

FetchContent_Declare(
saucer
GIT_REPOSITORY "https://github.com/saucer/saucer"
GIT_TAG v$VERSION$
)

FetchContent_MakeAvailable(saucer)

target_link_libraries(${PROJECT_NAME} PRIVATE saucer::saucer)
34 changes: 6 additions & 28 deletions docs/getting-started/basic-app.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
sidebar_position: 2
---

import { VersionedCode } from '../../src/components/CodeBlock';
import baseCmake from './basic-app.cmake';

# Your First Application

> On this page you'll learn how to setup your very first saucer project.
Expand All @@ -14,34 +17,9 @@ Make sure you've read the previous pages and have the system dependencies instal

In this example I'll use CMake as my build system of choice and make saucer available through [FetchContent](installation).

```cmake title="CMakeLists.txt"
cmake_minimum_required(VERSION 3.21)
project(your_awesome_app LANGUAGES CXX VERSION 1.0)
# --------------------------------------------------------------------------------------------------------
# Create executable
# --------------------------------------------------------------------------------------------------------
add_executable(${PROJECT_NAME} "main.cpp")
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 20 CXX_EXTENSIONS OFF CXX_STANDARD_REQUIRED ON)
# --------------------------------------------------------------------------------------------------------
# Link libraries
# --------------------------------------------------------------------------------------------------------
include(FetchContent)
FetchContent_Declare(
saucer
GIT_REPOSITORY "https://github.com/saucer/saucer"
GIT_TAG v2.0.0
)
FetchContent_MakeAvailable(saucer)
target_link_libraries(${PROJECT_NAME} PRIVATE saucer::saucer)
```
<VersionedCode title="CMakeLists.txt" language="cmake">
{baseCmake}
</VersionedCode>

## The Program

Expand Down
12 changes: 7 additions & 5 deletions docs/getting-started/installation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ sidebar_position: 0

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import { VersionedCode } from '../../src/components/CodeBlock';

# Installation

Expand All @@ -23,13 +24,14 @@ To get started choose your desired installation method below.
I recommend [CPM](https://github.com/cpm-cmake/CPM.cmake) for managing dependencies with CMake.
Please refer to their [docs](https://github.com/cpm-cmake/CPM.cmake#adding-cpm) on how to install it.

```cmake
CPMFindPackage(

<VersionedCode language="cmake">
{`CPMFindPackage(
NAME saucer
VERSION 2.0.0
VERSION $VERSION$
GIT_REPOSITORY "https://github.com/saucer/saucer"
)
```
)`}
</VersionedCode>

Now you can simply link your project against saucer.

Expand Down
27 changes: 27 additions & 0 deletions src/components/CodeBlock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import CodeBlock, { Props } from "@theme/CodeBlock";
import React, { useEffect, useState } from "react";

export function VersionedCode({ children, ...props }: Props)
{
const [version, setVersion] = useState("v2.0.1");

useEffect(() =>
{
fetch("https://api.github.com/repos/saucer/saucer/releases/latest", { headers: [
["Accept", "application/vnd.github+json"],
["X-GitHub-Api-Version", "2022-11-28"]
] }).then(data =>
{
data.json().then(json =>
{
setVersion(json["tag_name"] ?? version);
});
});


}, []);

return <CodeBlock {...props}>
{children.toString().replace("$VERSION$", version.replace(/[^0-9.]/, ""))}
</CodeBlock>;
}

0 comments on commit c68a915

Please sign in to comment.