Skip to content

Commit

Permalink
📦 Remove PACKAGED constant
Browse files Browse the repository at this point in the history
Align with appsettings.json behavior.
  • Loading branch information
database64128 committed Apr 24, 2024
1 parent 8b079e8 commit 39e68c6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 51 deletions.
6 changes: 4 additions & 2 deletions CubicBot.Telegram.App/CubicBot.Telegram.App.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<ItemGroup>
<ProjectReference Include="..\CubicBot.Telegram\CubicBot.Telegram.csproj" />
<ItemGroup>
<ProjectReference Include="..\CubicBot.Telegram\CubicBot.Telegram.csproj" />
</ItemGroup>

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<AssemblyName>cubic-bot-telegram-app</AssemblyName>
<Version>1.2.0</Version>
</PropertyGroup>

</Project>
10 changes: 6 additions & 4 deletions CubicBot.Telegram.Tool/CubicBot.Telegram.Tool.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\CubicBot.Telegram\CubicBot.Telegram.csproj" />
<ItemGroup>
<ProjectReference Include="..\CubicBot.Telegram\CubicBot.Telegram.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.24209.3" />
<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.24209.3" />
</ItemGroup>

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>cubic-bot-telegram-tool</AssemblyName>
<Version>1.2.0</Version>
</PropertyGroup>

</Project>
1 change: 0 additions & 1 deletion CubicBot.Telegram/CubicBot.Telegram.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyName>cubic-bot-telegram</AssemblyName>
<Authors>database64128</Authors>
<Copyright>© 2024 database64128</Copyright>
<Description>A stupid and annoying chatbot for your group chats.</Description>
Expand Down
28 changes: 1 addition & 27 deletions CubicBot.Telegram/Utils/FileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,6 @@ namespace CubicBot.Telegram.Utils;

public static class FileHelper
{
private static readonly string s_configDirectory;

static FileHelper()
{
#if PACKAGED
// ~/.config on Linux
// ~/AppData/Roaming on Windows
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
s_configDirectory = Path.Join(appDataPath, "cubic-bot-telegram");
#else
// Use executable directory
// Executable directory for single-file deployments in .NET 5+: https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file
s_configDirectory = AppContext.BaseDirectory;
#endif
}

/// <summary>
/// Gets the absolute path pointed to by the specified path.
/// </summary>
/// <param name="path">A relative or absolute path.</param>
/// <returns>A fully qualified path.</returns>
public static string GetAbsolutePath(string path) => Path.Combine(s_configDirectory, path);

/// <summary>
/// Loads the specified JSON file and deserializes its content as a <typeparamref name="TValue"/>.
/// </summary>
Expand All @@ -36,9 +13,8 @@ static FileHelper()
/// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
/// <param name="cancellationToken">A token that may be used to cancel the read operation.</param>
/// <returns>A <typeparamref name="TValue"/>.</returns>
public static async Task<TValue> LoadFromJsonFileAsync<TValue>(string path, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default) where TValue : class, new()
public static async ValueTask<TValue> LoadFromJsonFileAsync<TValue>(string path, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default) where TValue : class, new()
{
path = GetAbsolutePath(path);
if (!File.Exists(path))
return new();

Expand All @@ -61,8 +37,6 @@ public static async Task SaveToJsonFileAsync<TValue>(
JsonTypeInfo<TValue> jsonTypeInfo,
CancellationToken cancellationToken = default)
{
path = GetAbsolutePath(path);

var directoryPath = Path.GetDirectoryName(path);
if (string.IsNullOrEmpty(directoryPath))
throw new ArgumentException("Invalid path.", nameof(path));
Expand Down
26 changes: 9 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ Counts text messages and generates the talkative leaderboard.

Prerequisites: .NET 8 SDK

Note for packagers: The application by default uses executable directory as config directory.
To use user's config directory, define the constant `PACKAGED` when building.

### Build with Release configuration

```bash
Expand All @@ -123,27 +120,22 @@ dotnet build -c Release
### Publish as framework-dependent

```bash
dotnet publish CubicBot.Telegram -c Release
dotnet publish CubicBot.Telegram.App -c Release
dotnet publish CubicBot.Telegram.Tool -c Release
```

### Publish as self-contained for Linux x64

```bash
dotnet publish CubicBot.Telegram -c Release \
dotnet publish CubicBot.Telegram.App -c Release \
-p:PublishSingleFile=true \
-p:PublishTrimmed=true \
-p:DebuggerSupport=false \
-p:EnableUnsafeBinaryFormatterSerialization=false \
-p:EnableUnsafeUTF7Encoding=false \
-p:InvariantGlobalization=true \
-r linux-x64 --self-contained
```

### Publish as self-contained for packaging on Linux x64

```bash
dotnet publish CubicBot.Telegram -c Release \
-p:DefineConstants=PACKAGED \
dotnet publish CubicBot.Telegram.Tool -c Release \
-p:PublishSingleFile=true \
-p:PublishTrimmed=true \
-p:DebuggerSupport=false \
Expand All @@ -157,19 +149,19 @@ dotnet publish CubicBot.Telegram -c Release \

```bash
# See usage guide.
$ cubic-bot-telegram --help
$ cubic-bot-telegram-tool --help

# Set bot token in config.
$ cubic-bot-telegram config set --bot-token "1234567:4TT8bAc8GHUspu3ERYn-KGcvsvGB9u_n4ddy"
$ cubic-bot-telegram-tool config set --bot-token "1234567:4TT8bAc8GHUspu3ERYn-KGcvsvGB9u_n4ddy"

# Disable all stats.
$ cubic-bot-telegram config set --enable-stats-mod false
$ cubic-bot-telegram-tool config set --enable-stats-mod false

# Check config.
$ cubic-bot-telegram config get
$ cubic-bot-telegram-tool config get

# Start bot.
$ cubic-bot-telegram
$ cubic-bot-telegram-app
```

## License
Expand Down

0 comments on commit 39e68c6

Please sign in to comment.